blob: e59aabd0eae4f9a63559672914db90d5a54c97b6 [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>
Scott James Remnant95ba4342009-03-16 15:31:10 +010027#include <linux/net.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080028#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/sock.h>
32#include <net/route.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080033#include <net/netfilter/nf_queue.h>
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -080034#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define IPQ_QMAX_DEFAULT 1024
37#define IPQ_PROC_FS_NAME "ip_queue"
38#define NET_IPQ_QMAX 2088
39#define NET_IPQ_QMAX_NAME "ip_queue_maxlen"
40
Patrick McHardy02f014d2007-12-05 01:26:33 -080041typedef int (*ipq_cmpfn)(struct nf_queue_entry *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Brian Haley1192e402006-09-20 12:03:46 -070043static unsigned char copy_mode __read_mostly = IPQ_COPY_NONE;
Brian Haley94aec082006-09-18 00:05:22 -070044static unsigned int queue_maxlen __read_mostly = IPQ_QMAX_DEFAULT;
Eric Dumazet5756d342010-06-09 15:47:41 +020045static DEFINE_SPINLOCK(queue_lock);
Brian Haley1192e402006-09-20 12:03:46 -070046static int peer_pid __read_mostly;
47static unsigned int copy_range __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static unsigned int queue_total;
49static unsigned int queue_dropped = 0;
50static unsigned int queue_user_dropped = 0;
Brian Haley1192e402006-09-20 12:03:46 -070051static struct sock *ipqnl __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static LIST_HEAD(queue_list);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080053static DEFINE_MUTEX(ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -080056__ipq_enqueue_entry(struct nf_queue_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Patrick McHardy0ac41e82007-12-05 01:25:03 -080058 list_add_tail(&entry->list, &queue_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 queue_total++;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static inline int
63__ipq_set_mode(unsigned char mode, unsigned int range)
64{
65 int status = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 switch(mode) {
68 case IPQ_COPY_NONE:
69 case IPQ_COPY_META:
70 copy_mode = mode;
71 copy_range = 0;
72 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 case IPQ_COPY_PACKET:
Eric Dumazet5756d342010-06-09 15:47:41 +020075 if (range > 0xFFFF)
76 range = 0xFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 copy_range = range;
Eric Dumazet5756d342010-06-09 15:47:41 +020078 copy_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 default:
82 status = -EINVAL;
83
84 }
85 return status;
86}
87
Patrick McHardy95214092007-12-05 01:25:46 -080088static void __ipq_flush(ipq_cmpfn cmpfn, unsigned long data);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static inline void
91__ipq_reset(void)
92{
93 peer_pid = 0;
94 net_disable_timestamp();
95 __ipq_set_mode(IPQ_COPY_NONE, 0);
Patrick McHardy95214092007-12-05 01:25:46 -080096 __ipq_flush(NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Patrick McHardy02f014d2007-12-05 01:26:33 -080099static struct nf_queue_entry *
Patrick McHardy95214092007-12-05 01:25:46 -0800100ipq_find_dequeue_entry(unsigned long id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800102 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900103
Eric Dumazet5756d342010-06-09 15:47:41 +0200104 spin_lock_bh(&queue_lock);
Patrick McHardy95214092007-12-05 01:25:46 -0800105
106 list_for_each_entry(i, &queue_list, list) {
107 if ((unsigned long)i == id) {
108 entry = i;
109 break;
110 }
111 }
112
113 if (entry) {
114 list_del(&entry->list);
115 queue_total--;
116 }
117
Eric Dumazet5756d342010-06-09 15:47:41 +0200118 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return entry;
120}
121
122static void
Patrick McHardy95214092007-12-05 01:25:46 -0800123__ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
124{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800125 struct nf_queue_entry *entry, *next;
Patrick McHardy95214092007-12-05 01:25:46 -0800126
127 list_for_each_entry_safe(entry, next, &queue_list, list) {
128 if (!cmpfn || cmpfn(entry, data)) {
129 list_del(&entry->list);
130 queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800131 nf_reinject(entry, NF_DROP);
Patrick McHardy95214092007-12-05 01:25:46 -0800132 }
133 }
134}
135
136static void
137ipq_flush(ipq_cmpfn cmpfn, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Eric Dumazet5756d342010-06-09 15:47:41 +0200139 spin_lock_bh(&queue_lock);
Patrick McHardy95214092007-12-05 01:25:46 -0800140 __ipq_flush(cmpfn, data);
Eric Dumazet5756d342010-06-09 15:47:41 +0200141 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static struct sk_buff *
Patrick McHardy02f014d2007-12-05 01:26:33 -0800145ipq_build_packet_message(struct nf_queue_entry *entry, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700147 sk_buff_data_t old_tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 size_t size = 0;
149 size_t data_len = 0;
150 struct sk_buff *skb;
151 struct ipq_packet_msg *pmsg;
152 struct nlmsghdr *nlh;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700153 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric Dumazet5756d342010-06-09 15:47:41 +0200155 switch (ACCESS_ONCE(copy_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 case IPQ_COPY_META:
157 case IPQ_COPY_NONE:
158 size = NLMSG_SPACE(sizeof(*pmsg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case IPQ_COPY_PACKET:
Herbert Xu9e56c212010-04-08 14:52:28 +0200162 if (entry->skb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazet5756d342010-06-09 15:47:41 +0200163 (*errp = skb_checksum_help(entry->skb)))
Patrick McHardy66a79a12005-08-23 10:10:35 -0700164 return NULL;
Eric Dumazet5756d342010-06-09 15:47:41 +0200165
166 data_len = ACCESS_ONCE(copy_range);
167 if (data_len == 0 || data_len > entry->skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 data_len = entry->skb->len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 size = NLMSG_SPACE(sizeof(*pmsg) + data_len);
171 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 default:
174 *errp = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return NULL;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 skb = alloc_skb(size, GFP_ATOMIC);
179 if (!skb)
180 goto nlmsg_failure;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900181
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700182 old_tail = skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
184 pmsg = NLMSG_DATA(nlh);
185 memset(pmsg, 0, sizeof(*pmsg));
186
187 pmsg->packet_id = (unsigned long )entry;
188 pmsg->data_len = data_len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700189 tv = ktime_to_timeval(entry->skb->tstamp);
190 pmsg->timestamp_sec = tv.tv_sec;
191 pmsg->timestamp_usec = tv.tv_usec;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800192 pmsg->mark = entry->skb->mark;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800193 pmsg->hook = entry->hook;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 pmsg->hw_protocol = entry->skb->protocol;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900195
Patrick McHardy02f014d2007-12-05 01:26:33 -0800196 if (entry->indev)
197 strcpy(pmsg->indev_name, entry->indev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 else
199 pmsg->indev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900200
Patrick McHardy02f014d2007-12-05 01:26:33 -0800201 if (entry->outdev)
202 strcpy(pmsg->outdev_name, entry->outdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 else
204 pmsg->outdev_name[0] = '\0';
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900205
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200206 if (entry->indev && entry->skb->dev &&
207 entry->skb->mac_header != entry->skb->network_header) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 pmsg->hw_type = entry->skb->dev->type;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700209 pmsg->hw_addrlen = dev_parse_header(entry->skb,
210 pmsg->hw_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (data_len)
214 if (skb_copy_bits(entry->skb, 0, pmsg->payload, data_len))
215 BUG();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 nlh->nlmsg_len = skb->tail - old_tail;
218 return skb;
219
220nlmsg_failure:
Jesper Juhl91c66c62011-07-29 16:38:49 +0200221 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *errp = -EINVAL;
223 printk(KERN_ERR "ip_queue: error creating packet message\n");
224 return NULL;
225}
226
227static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800228ipq_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int status = -EINVAL;
231 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (copy_mode == IPQ_COPY_NONE)
234 return -EAGAIN;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 nskb = ipq_build_packet_message(entry, &status);
237 if (nskb == NULL)
Patrick McHardy02f014d2007-12-05 01:26:33 -0800238 return status;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900239
Eric Dumazet5756d342010-06-09 15:47:41 +0200240 spin_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (!peer_pid)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900243 goto err_out_free_nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (queue_total >= queue_maxlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900246 queue_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 status = -ENOSPC;
248 if (net_ratelimit())
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900249 printk (KERN_WARNING "ip_queue: full at %d entries, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 "dropping packets(s). Dropped: %d\n", queue_total,
251 queue_dropped);
252 goto err_out_free_nskb;
253 }
254
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900255 /* netlink_unicast will either free the nskb or attach it to a socket */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 status = netlink_unicast(ipqnl, nskb, peer_pid, MSG_DONTWAIT);
257 if (status < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900258 queue_user_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto err_out_unlock;
260 }
261
262 __ipq_enqueue_entry(entry);
263
Eric Dumazet5756d342010-06-09 15:47:41 +0200264 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return status;
266
267err_out_free_nskb:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900268 kfree_skb(nskb);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270err_out_unlock:
Eric Dumazet5756d342010-06-09 15:47:41 +0200271 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return status;
273}
274
275static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800276ipq_mangle_ipv4(ipq_verdict_msg_t *v, struct nf_queue_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 int diff;
279 struct iphdr *user_iph = (struct iphdr *)v->payload;
Patrick McHardye2b58a62008-02-19 17:17:52 -0800280 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (v->data_len < sizeof(*user_iph))
283 return 0;
284 diff = v->data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800285 if (diff < 0) {
286 if (pskb_trim(e->skb, v->data_len))
287 return -ENOMEM;
288 } else if (diff > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (v->data_len > 0xFFFF)
290 return -EINVAL;
291 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700292 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
293 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800294 if (!nskb) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700295 printk(KERN_WARNING "ip_queue: error "
Patrick McHardye2b58a62008-02-19 17:17:52 -0800296 "in mangle, dropping packet\n");
297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800299 kfree_skb(e->skb);
300 e->skb = nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 skb_put(e->skb, diff);
303 }
Herbert Xu37d41872007-10-14 00:39:18 -0700304 if (!skb_make_writable(e->skb, v->data_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300306 skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
Patrick McHardy66a79a12005-08-23 10:10:35 -0700307 e->skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312static int
313ipq_set_verdict(struct ipq_verdict_msg *vmsg, unsigned int len)
314{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800315 struct nf_queue_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Florian Westphalc6675232011-08-30 15:01:20 +0200317 if (vmsg->value > NF_MAX_VERDICT || vmsg->value == NF_STOLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EINVAL;
319
Patrick McHardy95214092007-12-05 01:25:46 -0800320 entry = ipq_find_dequeue_entry(vmsg->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (entry == NULL)
322 return -ENOENT;
323 else {
324 int verdict = vmsg->value;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (vmsg->data_len && vmsg->data_len == len)
327 if (ipq_mangle_ipv4(vmsg, entry) < 0)
328 verdict = NF_DROP;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900329
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800330 nf_reinject(entry, verdict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332 }
333}
334
335static int
336ipq_set_mode(unsigned char mode, unsigned int range)
337{
338 int status;
339
Eric Dumazet5756d342010-06-09 15:47:41 +0200340 spin_lock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = __ipq_set_mode(mode, range);
Eric Dumazet5756d342010-06-09 15:47:41 +0200342 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return status;
344}
345
346static int
347ipq_receive_peer(struct ipq_peer_msg *pmsg,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900348 unsigned char type, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int status = 0;
351
352 if (len < sizeof(*pmsg))
353 return -EINVAL;
354
355 switch (type) {
356 case IPQM_MODE:
357 status = ipq_set_mode(pmsg->msg.mode.value,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900358 pmsg->msg.mode.range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 case IPQM_VERDICT:
Florian Westphalc6675232011-08-30 15:01:20 +0200362 status = ipq_set_verdict(&pmsg->msg.verdict,
363 len - sizeof(*pmsg));
364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 default:
366 status = -EINVAL;
367 }
368 return status;
369}
370
371static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800372dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800374 if (entry->indev)
375 if (entry->indev->ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800377 if (entry->outdev)
378 if (entry->outdev->ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700380#ifdef CONFIG_BRIDGE_NETFILTER
381 if (entry->skb->nf_bridge) {
382 if (entry->skb->nf_bridge->physindev &&
383 entry->skb->nf_bridge->physindev->ifindex == ifindex)
384 return 1;
385 if (entry->skb->nf_bridge->physoutdev &&
386 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900387 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700388 }
389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
393static void
394ipq_dev_drop(int ifindex)
395{
Patrick McHardy95214092007-12-05 01:25:46 -0800396 ipq_flush(dev_cmp, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
400
401static inline void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700402__ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Dave Jonesd232b8d2011-05-27 20:36:51 -0400404 int status, type, pid, flags;
405 unsigned int nlmsglen, skblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct nlmsghdr *nlh;
407
408 skblen = skb->len;
409 if (skblen < sizeof(*nlh))
410 return;
411
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700412 nlh = nlmsg_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 nlmsglen = nlh->nlmsg_len;
414 if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
415 return;
416
417 pid = nlh->nlmsg_pid;
418 flags = nlh->nlmsg_flags;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if(pid <= 0 || !(flags & NLM_F_REQUEST) || flags & NLM_F_MULTI)
421 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (flags & MSG_TRUNC)
424 RCV_SKB_FAIL(-ECOMM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 type = nlh->nlmsg_type;
427 if (type < NLMSG_NOOP || type >= IPQM_MAX)
428 RCV_SKB_FAIL(-EINVAL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (type <= IPQM_BASE)
431 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900432
Darrel Goeddelc7bdb542006-06-27 13:26:11 -0700433 if (security_netlink_recv(skb, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 RCV_SKB_FAIL(-EPERM);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900435
Eric Dumazet5756d342010-06-09 15:47:41 +0200436 spin_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (peer_pid) {
439 if (peer_pid != pid) {
Eric Dumazet5756d342010-06-09 15:47:41 +0200440 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 RCV_SKB_FAIL(-EBUSY);
442 }
443 } else {
444 net_enable_timestamp();
445 peer_pid = pid;
446 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900447
Eric Dumazet5756d342010-06-09 15:47:41 +0200448 spin_unlock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 status = ipq_receive_peer(NLMSG_DATA(nlh), type,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900451 nlmsglen - NLMSG_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (status < 0)
453 RCV_SKB_FAIL(status);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (flags & NLM_F_ACK)
456 netlink_ack(skb, nlh, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700460ipq_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800462 mutex_lock(&ipqnl_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700463 __ipq_rcv_skb(skb);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800464 mutex_unlock(&ipqnl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467static int
468ipq_rcv_dev_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900469 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct net_device *dev = ptr;
472
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700473 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200474 return NOTIFY_DONE;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Drop any packets associated with the downed device */
477 if (event == NETDEV_DOWN)
478 ipq_dev_drop(dev->ifindex);
479 return NOTIFY_DONE;
480}
481
482static struct notifier_block ipq_dev_notifier = {
483 .notifier_call = ipq_rcv_dev_event,
484};
485
486static int
487ipq_rcv_nl_event(struct notifier_block *this,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900488 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct netlink_notify *n = ptr;
491
Patrick McHardydee58172009-11-06 17:04:00 +0100492 if (event == NETLINK_URELEASE && n->protocol == NETLINK_FIREWALL) {
Eric Dumazet5756d342010-06-09 15:47:41 +0200493 spin_lock_bh(&queue_lock);
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800494 if ((net_eq(n->net, &init_net)) && (n->pid == peer_pid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 __ipq_reset();
Eric Dumazet5756d342010-06-09 15:47:41 +0200496 spin_unlock_bh(&queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 return NOTIFY_DONE;
499}
500
501static struct notifier_block ipq_nl_notifier = {
502 .notifier_call = ipq_rcv_nl_event,
503};
504
Patrick McHardyc392a742008-01-31 04:54:18 -0800505#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static struct ctl_table_header *ipq_sysctl_header;
507
508static ctl_table ipq_table[] = {
509 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .procname = NET_IPQ_QMAX_NAME,
511 .data = &queue_maxlen,
512 .maxlen = sizeof(queue_maxlen),
513 .mode = 0644,
514 .proc_handler = proc_dointvec
515 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800516 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517};
Patrick McHardyc392a742008-01-31 04:54:18 -0800518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Patrick McHardyc392a742008-01-31 04:54:18 -0800520#ifdef CONFIG_PROC_FS
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800521static int ip_queue_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Eric Dumazet5756d342010-06-09 15:47:41 +0200523 spin_lock_bh(&queue_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900524
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800525 seq_printf(m,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900526 "Peer PID : %d\n"
527 "Copy mode : %hu\n"
528 "Copy range : %u\n"
529 "Queue length : %u\n"
530 "Queue max. length : %u\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 "Queue dropped : %u\n"
532 "Netlink dropped : %u\n",
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900533 peer_pid,
534 copy_mode,
535 copy_range,
536 queue_total,
537 queue_maxlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 queue_dropped,
539 queue_user_dropped);
540
Eric Dumazet5756d342010-06-09 15:47:41 +0200541 spin_unlock_bh(&queue_lock);
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Alexey Dobriyan7351a222007-11-05 20:33:46 -0800544
545static int ip_queue_open(struct inode *inode, struct file *file)
546{
547 return single_open(file, ip_queue_show, NULL);
548}
549
550static const struct file_operations ip_queue_proc_fops = {
551 .open = ip_queue_open,
552 .read = seq_read,
553 .llseek = seq_lseek,
554 .release = single_release,
555 .owner = THIS_MODULE,
556};
Patrick McHardyc392a742008-01-31 04:54:18 -0800557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Patrick McHardye3ac5292007-12-05 01:23:57 -0800559static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700560 .name = "ip_queue",
561 .outfn = &ipq_enqueue_packet,
562};
563
Patrick McHardy32292a72006-04-06 14:11:30 -0700564static int __init ip_queue_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 int status = -ENOMEM;
Patrick McHardyc392a742008-01-31 04:54:18 -0800567 struct proc_dir_entry *proc __maybe_unused;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 netlink_register_notifier(&ipq_nl_notifier);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200570 ipqnl = netlink_kernel_create(&init_net, NETLINK_FIREWALL, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700571 ipq_rcv_skb, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (ipqnl == NULL) {
573 printk(KERN_ERR "ip_queue: failed to create netlink socket\n");
574 goto cleanup_netlink_notifier;
575 }
576
Patrick McHardyc392a742008-01-31 04:54:18 -0800577#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700578 proc = proc_create(IPQ_PROC_FS_NAME, 0, init_net.proc_net,
579 &ip_queue_proc_fops);
580 if (!proc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 printk(KERN_ERR "ip_queue: failed to create proc entry\n");
582 goto cleanup_ipqnl;
583 }
Patrick McHardyc392a742008-01-31 04:54:18 -0800584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 register_netdevice_notifier(&ipq_dev_notifier);
Patrick McHardyc392a742008-01-31 04:54:18 -0800586#ifdef CONFIG_SYSCTL
Pavel Emelyanov3d7cc2b2008-01-09 00:33:11 -0800587 ipq_sysctl_header = register_sysctl_paths(net_ipv4_ctl_path, ipq_table);
Patrick McHardyc392a742008-01-31 04:54:18 -0800588#endif
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200589 status = nf_register_queue_handler(NFPROTO_IPV4, &nfqh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (status < 0) {
591 printk(KERN_ERR "ip_queue: failed to register queue handler\n");
592 goto cleanup_sysctl;
593 }
594 return status;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596cleanup_sysctl:
Patrick McHardyc392a742008-01-31 04:54:18 -0800597#ifdef CONFIG_SYSCTL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unregister_sysctl_table(ipq_sysctl_header);
Patrick McHardyc392a742008-01-31 04:54:18 -0800599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200601 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardyc392a742008-01-31 04:54:18 -0800602cleanup_ipqnl: __maybe_unused
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800603 netlink_kernel_release(ipqnl);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800604 mutex_lock(&ipqnl_mutex);
605 mutex_unlock(&ipqnl_mutex);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607cleanup_netlink_notifier:
608 netlink_unregister_notifier(&ipq_nl_notifier);
609 return status;
610}
611
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800612static void __exit ip_queue_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Patrick McHardy32292a72006-04-06 14:11:30 -0700614 nf_unregister_queue_handlers(&nfqh);
Eric Dumazet94817212009-11-04 21:14:31 +0100615
Patrick McHardy95214092007-12-05 01:25:46 -0800616 ipq_flush(NULL, 0);
Patrick McHardy32292a72006-04-06 14:11:30 -0700617
Patrick McHardyc392a742008-01-31 04:54:18 -0800618#ifdef CONFIG_SYSCTL
Patrick McHardy32292a72006-04-06 14:11:30 -0700619 unregister_sysctl_table(ipq_sysctl_header);
Patrick McHardyc392a742008-01-31 04:54:18 -0800620#endif
Patrick McHardy32292a72006-04-06 14:11:30 -0700621 unregister_netdevice_notifier(&ipq_dev_notifier);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200622 proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
Patrick McHardy32292a72006-04-06 14:11:30 -0700623
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800624 netlink_kernel_release(ipqnl);
Patrick McHardy32292a72006-04-06 14:11:30 -0700625 mutex_lock(&ipqnl_mutex);
626 mutex_unlock(&ipqnl_mutex);
627
628 netlink_unregister_notifier(&ipq_nl_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631MODULE_DESCRIPTION("IPv4 packet queue handler");
632MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
633MODULE_LICENSE("GPL");
Scott James Remnant95ba4342009-03-16 15:31:10 +0100634MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_FIREWALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800636module_init(ip_queue_init);
637module_exit(ip_queue_fini);