blob: 2966fbddce8be951b59454df5c99ee256523299e [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{
76 list_add(&entry->list, &queue_list);
77 queue_total++;
78}
79
80/*
81 * Find and return a queued entry matched by cmpfn, or return the last
82 * entry if cmpfn is NULL.
83 */
84static inline struct ipq_queue_entry *
85__ipq_find_entry(ipq_cmpfn cmpfn, unsigned long data)
86{
87 struct list_head *p;
88
89 list_for_each_prev(p, &queue_list) {
90 struct ipq_queue_entry *entry = (struct ipq_queue_entry *)p;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (!cmpfn || cmpfn(entry, data))
93 return entry;
94 }
95 return NULL;
96}
97
98static inline void
99__ipq_dequeue_entry(struct ipq_queue_entry *entry)
100{
101 list_del(&entry->list);
102 queue_total--;
103}
104
105static inline struct ipq_queue_entry *
106__ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
107{
108 struct ipq_queue_entry *entry;
109
110 entry = __ipq_find_entry(cmpfn, data);
111 if (entry == NULL)
112 return NULL;
113
114 __ipq_dequeue_entry(entry);
115 return entry;
116}
117
118
119static inline void
120__ipq_flush(int verdict)
121{
122 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 while ((entry = __ipq_find_dequeue_entry(NULL, 0)))
125 ipq_issue_verdict(entry, verdict);
126}
127
128static inline int
129__ipq_set_mode(unsigned char mode, unsigned int range)
130{
131 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 switch(mode) {
134 case IPQ_COPY_NONE:
135 case IPQ_COPY_META:
136 copy_mode = mode;
137 copy_range = 0;
138 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 case IPQ_COPY_PACKET:
141 copy_mode = mode;
142 copy_range = range;
143 if (copy_range > 0xFFFF)
144 copy_range = 0xFFFF;
145 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 default:
148 status = -EINVAL;
149
150 }
151 return status;
152}
153
154static inline void
155__ipq_reset(void)
156{
157 peer_pid = 0;
158 net_disable_timestamp();
159 __ipq_set_mode(IPQ_COPY_NONE, 0);
160 __ipq_flush(NF_DROP);
161}
162
163static struct ipq_queue_entry *
164ipq_find_dequeue_entry(ipq_cmpfn cmpfn, unsigned long data)
165{
166 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 write_lock_bh(&queue_lock);
169 entry = __ipq_find_dequeue_entry(cmpfn, data);
170 write_unlock_bh(&queue_lock);
171 return entry;
172}
173
174static void
175ipq_flush(int verdict)
176{
177 write_lock_bh(&queue_lock);
178 __ipq_flush(verdict);
179 write_unlock_bh(&queue_lock);
180}
181
182static struct sk_buff *
183ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
184{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700185 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 size_t size = 0;
187 size_t data_len = 0;
188 struct sk_buff *skb;
189 struct ipq_packet_msg *pmsg;
190 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700191 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 switch (copy_mode) {
196 case IPQ_COPY_META:
197 case IPQ_COPY_NONE:
198 size = NLMSG_SPACE(sizeof(*pmsg));
199 data_len = 0;
200 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 case IPQ_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700203 if ((entry->skb->ip_summed == CHECKSUM_PARTIAL ||
204 entry->skb->ip_summed == CHECKSUM_COMPLETE) &&
205 (*errp = skb_checksum_help(entry->skb))) {
Patrick McHardy66a79a12005-08-23 10:10:35 -0700206 read_unlock_bh(&queue_lock);
207 return NULL;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (copy_range == 0 || copy_range > entry->skb->len)
210 data_len = entry->skb->len;
211 else
212 data_len = copy_range;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
215 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 *errp = -EINVAL;
219 read_unlock_bh(&queue_lock);
220 return NULL;
221 }
222
223 read_unlock_bh(&queue_lock);
224
225 skb = alloc_skb(size, GFP_ATOMIC);
226 if (!skb)
227 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900228
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700229 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
231 pmsg = NLMSG_DATA(nlh);
232 memset(pmsg, 0, sizeof(*pmsg));
233
234 pmsg->packet_id = (unsigned long )entry;
235 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700236 tv = ktime_to_timeval(entry->skb->tstamp);
237 pmsg->timestamp_sec = tv.tv_sec;
238 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800239 pmsg->mark = entry->skb->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 pmsg->hook = entry->info->hook;
241 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (entry->info->indev)
244 strcpy(pmsg->indev_name, entry->info->indev->name);
245 else
246 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (entry->info->outdev)
249 strcpy(pmsg->outdev_name, entry->info->outdev->name);
250 else
251 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (entry->info->indev && entry->skb->dev) {
254 pmsg->hw_type = entry->skb->dev->type;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700255 pmsg->hw_addrlen = dev_parse_header(entry->skb,
256 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (data_len)
260 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
261 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 nlh->nlmsg_len = skb->tail - old_tail;
264 return skb;
265
266nlmsg_failure:
267 if (skb)
268 kfree_skb(skb);
269 *errp = -EINVAL;
270 printk(KERN_ERR "ip_queue: error creating packet message\n");
271 return NULL;
272}
273
274static int
Harald Welte0ab43f82005-08-09 19:43:44 -0700275ipq_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Patrick McHardyf9d89282007-12-05 01:24:30 -0800276 unsigned int queuenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int status = -EINVAL;
279 struct sk_buff *nskb;
280 struct ipq_queue_entry *entry;
281
282 if (copy_mode == IPQ_COPY_NONE)
283 return -EAGAIN;
284
285 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
286 if (entry == NULL) {
287 printk(KERN_ERR "ip_queue: OOM in ipq_enqueue_packet()\n");
288 return -ENOMEM;
289 }
290
291 entry->info = info;
292 entry->skb = skb;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 nskb = ipq_build_packet_message(entry, &status);
295 if (nskb == NULL)
296 goto err_out_free;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900301 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900304 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 status = -ENOSPC;
306 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900307 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "dropping packets(s). Dropped: %d\n", queue_total,
309 queue_dropped);
310 goto err_out_free_nskb;
311 }
312
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900313 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
315 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900316 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto err_out_unlock;
318 }
319
320 __ipq_enqueue_entry(entry);
321
322 write_unlock_bh(&queue_lock);
323 return status;
324
325err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900326 kfree_skb(nskb);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328err_out_unlock:
329 write_unlock_bh(&queue_lock);
330
331err_out_free:
332 kfree(entry);
333 return status;
334}
335
336static int
337ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
338{
339 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700340 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct iphdr *user_iph = (struct iphdr *)v->payload;
342
343 if (v->data_len < sizeof(*user_iph))
344 return 0;
345 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800346 if (diff < 0) {
347 if (pskb_trim(e->skb, v->data_len))
348 return -ENOMEM;
349 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (v->data_len > 0xFFFF)
351 return -EINVAL;
352 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700353 err = pskb_expand_head(e->skb, 0,
354 diff - skb_tailroom(e->skb),
355 GFP_ATOMIC);
356 if (err) {
357 printk(KERN_WARNING "ip_queue: error "
358 "in mangle, dropping packet: %d\n", -err);
359 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 skb_put(e->skb, diff);
363 }
Herbert Xu37d41872007-10-14 00:39:18 -0700364 if (!skb_make_writable(e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300366 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700367 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372static inline int
373id_cmp(struct ipq_queue_entry *e, unsigned long id)
374{
375 return (id == (unsigned long )e);
376}
377
378static int
379ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
380{
381 struct ipq_queue_entry *entry;
382
383 if (vmsg->value > NF_MAX_VERDICT)
384 return -EINVAL;
385
386 entry = ipq_find_dequeue_entry(id_cmp, vmsg->id);
387 if (entry == NULL)
388 return -ENOENT;
389 else {
390 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (vmsg->data_len && vmsg->data_len == len)
393 if (ipq_mangle_ipv4(vmsg, entry) < 0)
394 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ipq_issue_verdict(entry, verdict);
397 return 0;
398 }
399}
400
401static int
402ipq_set_mode(unsigned char mode, unsigned int range)
403{
404 int status;
405
406 write_lock_bh(&queue_lock);
407 status = __ipq_set_mode(mode, range);
408 write_unlock_bh(&queue_lock);
409 return status;
410}
411
412static int
413ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900414 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int status = 0;
417
418 if (len < sizeof(*pmsg))
419 return -EINVAL;
420
421 switch (type) {
422 case IPQM_MODE:
423 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900424 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 case IPQM_VERDICT:
428 if (pmsg->msg.verdict.value > NF_MAX_VERDICT)
429 status = -EINVAL;
430 else
431 status = ipq_set_verdict(&pmsg->msg.verdict,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900432 len - sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 default:
435 status = -EINVAL;
436 }
437 return status;
438}
439
440static int
441dev_cmp(struct ipq_queue_entry *entry, unsigned long ifindex)
442{
443 if (entry->info->indev)
444 if (entry->info->indev->ifindex == ifindex)
445 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (entry->info->outdev)
447 if (entry->info->outdev->ifindex == ifindex)
448 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700449#ifdef CONFIG_BRIDGE_NETFILTER
450 if (entry->skb->nf_bridge) {
451 if (entry->skb->nf_bridge->physindev &&
452 entry->skb->nf_bridge->physindev->ifindex == ifindex)
453 return 1;
454 if (entry->skb->nf_bridge->physoutdev &&
455 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900456 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700457 }
458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460}
461
462static void
463ipq_dev_drop(int ifindex)
464{
465 struct ipq_queue_entry *entry;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 while ((entry = ipq_find_dequeue_entry(dev_cmp, ifindex)) != NULL)
468 ipq_issue_verdict(entry, NF_DROP);
469}
470
471#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
472
473static inline void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700474__ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int status, type, pid, flags, nlmsglen, skblen;
477 struct nlmsghdr *nlh;
478
479 skblen = skb->len;
480 if (skblen < sizeof(*nlh))
481 return;
482
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700483 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 nlmsglen = nlh->nlmsg_len;
485 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
486 return;
487
488 pid = nlh->nlmsg_pid;
489 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
492 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (flags & MSG_TRUNC)
495 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 type = nlh->nlmsg_type;
498 if (type < NLMSG_NOOP || type >= IPQM_MAX)
499 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (type <= IPQM_BASE)
502 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900503
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700504 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 write_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (peer_pid) {
510 if (peer_pid != pid) {
511 write_unlock_bh(&queue_lock);
512 RCV_SKB_FAIL(-EBUSY);
513 }
514 } else {
515 net_enable_timestamp();
516 peer_pid = pid;
517 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 write_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900522 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (status < 0)
524 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (flags & NLM_F_ACK)
527 netlink_ack(skb, nlh, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900528 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700532ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800534 mutex_lock(&ipqnl_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700535 __ipq_rcv_skb(skb);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800536 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static int
540ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900541 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct net_device *dev = ptr;
544
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200545 if (dev->nd_net != &init_net)
546 return NOTIFY_DONE;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Drop any packets associated with the downed device */
549 if (event == NETDEV_DOWN)
550 ipq_dev_drop(dev->ifindex);
551 return NOTIFY_DONE;
552}
553
554static struct notifier_block ipq_dev_notifier = {
555 .notifier_call = ipq_rcv_dev_event,
556};
557
558static int
559ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900560 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct netlink_notify *n = ptr;
563
564 if (event == NETLINK_URELEASE &&
565 n->protocol == NETLINK_FIREWALL && n->pid) {
566 write_lock_bh(&queue_lock);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200567 if ((n->net == &init_net) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 __ipq_reset();
569 write_unlock_bh(&queue_lock);
570 }
571 return NOTIFY_DONE;
572}
573
574static struct notifier_block ipq_nl_notifier = {
575 .notifier_call = ipq_rcv_nl_event,
576};
577
578static struct ctl_table_header *ipq_sysctl_header;
579
580static ctl_table ipq_table[] = {
581 {
582 .ctl_name = NET_IPQ_QMAX,
583 .procname = NET_IPQ_QMAX_NAME,
584 .data = &queue_maxlen,
585 .maxlen = sizeof(queue_maxlen),
586 .mode = 0644,
587 .proc_handler = proc_dointvec
588 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900589 { .ctl_name = 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
592static ctl_table ipq_dir_table[] = {
593 {
594 .ctl_name = NET_IPV4,
595 .procname = "ipv4",
596 .mode = 0555,
597 .child = ipq_table
598 },
599 { .ctl_name = 0 }
600};
601
602static ctl_table ipq_root_table[] = {
603 {
604 .ctl_name = CTL_NET,
605 .procname = "net",
606 .mode = 0555,
607 .child = ipq_dir_table
608 },
609 { .ctl_name = 0 }
610};
611
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800612static int ip_queue_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 read_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900615
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800616 seq_printf(m,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900617 "Peer PID : %d\n"
618 "Copy mode : %hu\n"
619 "Copy range : %u\n"
620 "Queue length : %u\n"
621 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 "Queue dropped : %u\n"
623 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900624 peer_pid,
625 copy_mode,
626 copy_range,
627 queue_total,
628 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 queue_dropped,
630 queue_user_dropped);
631
632 read_unlock_bh(&queue_lock);
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800635
636static int ip_queue_open(struct inode *inode, struct file *file)
637{
638 return single_open(file, ip_queue_show, NULL);
639}
640
641static const struct file_operations ip_queue_proc_fops = {
642 .open = ip_queue_open,
643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = single_release,
646 .owner = THIS_MODULE,
647};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Patrick McHardye3ac5292007-12-05 01:23:57 -0800649static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700650 .name = "ip_queue",
651 .outfn = &ipq_enqueue_packet,
652};
653
Patrick McHardy32292a72006-04-06 14:11:30 -0700654static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 int status = -ENOMEM;
657 struct proc_dir_entry *proc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200660 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700661 ipq_rcv_skb, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (ipqnl == NULL) {
663 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
664 goto cleanup_netlink_notifier;
665 }
666
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800667 proc = create_proc_entry(IPQ_PROC_FS_NAME, 0, init_net.proc_net);
668 if (proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 proc->owner = THIS_MODULE;
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800670 proc->proc_fops = &ip_queue_proc_fops;
671 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
673 goto cleanup_ipqnl;
674 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 register_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800677 ipq_sysctl_header = register_sysctl_table(ipq_root_table);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900678
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700679 status = nf_register_queue_handler(PF_INET, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (status < 0) {
681 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
682 goto cleanup_sysctl;
683 }
684 return status;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686cleanup_sysctl:
687 unregister_sysctl_table(ipq_sysctl_header);
688 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200689 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690cleanup_ipqnl:
691 sock_release(ipqnl->sk_socket);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800692 mutex_lock(&ipqnl_mutex);
693 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695cleanup_netlink_notifier:
696 netlink_unregister_notifier(&ipq_nl_notifier);
697 return status;
698}
699
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800700static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Patrick McHardy32292a72006-04-06 14:11:30 -0700702 nf_unregister_queue_handlers(&nfqh);
703 synchronize_net();
704 ipq_flush(NF_DROP);
705
706 unregister_sysctl_table(ipq_sysctl_header);
707 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200708 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700709
710 sock_release(ipqnl->sk_socket);
711 mutex_lock(&ipqnl_mutex);
712 mutex_unlock(&ipqnl_mutex);
713
714 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717MODULE_DESCRIPTION("IPv4 packet queue handler");
718MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
719MODULE_LICENSE("GPL");
720
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800721module_init(ip_queue_init);
722module_exit(ip_queue_fini);