Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[deliverable/linux.git] / net / ipv4 / netfilter / nf_conntrack_proto_icmp.c
CommitLineData
9fb9cbb1
YK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - enable working with Layer 3 protocol independent connection tracking.
10 *
11 * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c
12 */
13
14#include <linux/types.h>
15#include <linux/sched.h>
16#include <linux/timer.h>
17#include <linux/netfilter.h>
18#include <linux/in.h>
19#include <linux/icmp.h>
20#include <linux/seq_file.h>
21#include <net/ip.h>
22#include <net/checksum.h>
23#include <linux/netfilter_ipv4.h>
24#include <net/netfilter/nf_conntrack_tuple.h>
605dcad6 25#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1
YK
26#include <net/netfilter/nf_conntrack_core.h>
27
933a41e7 28static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
9fb9cbb1
YK
29
30#if 0
31#define DEBUGP printk
32#else
33#define DEBUGP(format, args...)
34#endif
35
36static int icmp_pkt_to_tuple(const struct sk_buff *skb,
37 unsigned int dataoff,
38 struct nf_conntrack_tuple *tuple)
39{
40 struct icmphdr _hdr, *hp;
41
42 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
43 if (hp == NULL)
44 return 0;
45
46 tuple->dst.u.icmp.type = hp->type;
47 tuple->src.u.icmp.id = hp->un.echo.id;
48 tuple->dst.u.icmp.code = hp->code;
49
50 return 1;
51}
52
c1d10adb
PNA
53/* Add 1; spaces filled with 0. */
54static const u_int8_t invmap[] = {
55 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
56 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
57 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
58 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
59 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
60 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
61 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
62 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
63};
64
9fb9cbb1
YK
65static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
66 const struct nf_conntrack_tuple *orig)
67{
9fb9cbb1
YK
68 if (orig->dst.u.icmp.type >= sizeof(invmap)
69 || !invmap[orig->dst.u.icmp.type])
70 return 0;
71
72 tuple->src.u.icmp.id = orig->src.u.icmp.id;
73 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
74 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
75 return 1;
76}
77
78/* Print out the per-protocol part of the tuple. */
79static int icmp_print_tuple(struct seq_file *s,
80 const struct nf_conntrack_tuple *tuple)
81{
82 return seq_printf(s, "type=%u code=%u id=%u ",
83 tuple->dst.u.icmp.type,
84 tuple->dst.u.icmp.code,
85 ntohs(tuple->src.u.icmp.id));
86}
87
88/* Print out the private part of the conntrack. */
89static int icmp_print_conntrack(struct seq_file *s,
90 const struct nf_conn *conntrack)
91{
92 return 0;
93}
94
95/* Returns verdict for packet, or -1 for invalid. */
96static int icmp_packet(struct nf_conn *ct,
97 const struct sk_buff *skb,
98 unsigned int dataoff,
99 enum ip_conntrack_info ctinfo,
100 int pf,
101 unsigned int hooknum)
102{
103 /* Try to delete connection immediately after all replies:
e905a9ed
YH
104 won't actually vanish as we still have skb, and del_timer
105 means this will only run once even if count hits zero twice
106 (theoretically possible with SMP) */
9fb9cbb1
YK
107 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
108 if (atomic_dec_and_test(&ct->proto.icmp.count)
109 && del_timer(&ct->timeout))
110 ct->timeout.function((unsigned long)ct);
111 } else {
112 atomic_inc(&ct->proto.icmp.count);
113 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
114 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
115 }
116
117 return NF_ACCEPT;
118}
119
120/* Called when a new connection for this protocol found. */
121static int icmp_new(struct nf_conn *conntrack,
122 const struct sk_buff *skb, unsigned int dataoff)
123{
c1d10adb
PNA
124 static const u_int8_t valid_new[] = {
125 [ICMP_ECHO] = 1,
126 [ICMP_TIMESTAMP] = 1,
127 [ICMP_INFO_REQUEST] = 1,
128 [ICMP_ADDRESS] = 1
129 };
9fb9cbb1
YK
130
131 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
132 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
133 /* Can't create a new ICMP `conn' with this. */
134 DEBUGP("icmp: can't create new conn with type %u\n",
135 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
136 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
137 return 0;
138 }
139 atomic_set(&conntrack->proto.icmp.count, 0);
140 return 1;
141}
142
143extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
144/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
145static int
146icmp_error_message(struct sk_buff *skb,
e905a9ed
YH
147 enum ip_conntrack_info *ctinfo,
148 unsigned int hooknum)
9fb9cbb1
YK
149{
150 struct nf_conntrack_tuple innertuple, origtuple;
151 struct {
152 struct icmphdr icmp;
153 struct iphdr ip;
154 } _in, *inside;
605dcad6 155 struct nf_conntrack_l4proto *innerproto;
9fb9cbb1
YK
156 struct nf_conntrack_tuple_hash *h;
157 int dataoff;
158
159 NF_CT_ASSERT(skb->nfct == NULL);
160
161 /* Not enough header? */
162 inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
163 if (inside == NULL)
164 return -NF_ACCEPT;
165
166 /* Ignore ICMP's containing fragments (shouldn't happen) */
167 if (inside->ip.frag_off & htons(IP_OFFSET)) {
168 DEBUGP("icmp_error_message: fragment of proto %u\n",
169 inside->ip.protocol);
170 return -NF_ACCEPT;
171 }
172
923f4902 173 /* rcu_read_lock()ed by nf_hook_slow */
605dcad6 174 innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
923f4902 175
9fb9cbb1
YK
176 dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp);
177 /* Are they talking about one of our connections? */
178 if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET,
179 inside->ip.protocol, &origtuple,
180 &nf_conntrack_l3proto_ipv4, innerproto)) {
181 DEBUGP("icmp_error_message: ! get_tuple p=%u",
182 inside->ip.protocol);
183 return -NF_ACCEPT;
184 }
185
e905a9ed
YH
186 /* Ordinarily, we'd expect the inverted tupleproto, but it's
187 been preserved inside the ICMP. */
188 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
9fb9cbb1
YK
189 &nf_conntrack_l3proto_ipv4, innerproto)) {
190 DEBUGP("icmp_error_message: no match\n");
191 return -NF_ACCEPT;
192 }
193
194 *ctinfo = IP_CT_RELATED;
195
196 h = nf_conntrack_find_get(&innertuple, NULL);
197 if (!h) {
198 /* Locally generated ICMPs will match inverted if they
199 haven't been SNAT'ed yet */
200 /* FIXME: NAT code has to handle half-done double NAT --RR */
201 if (hooknum == NF_IP_LOCAL_OUT)
202 h = nf_conntrack_find_get(&origtuple, NULL);
203
204 if (!h) {
205 DEBUGP("icmp_error_message: no match\n");
206 return -NF_ACCEPT;
207 }
208
209 /* Reverse direction from that found */
210 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
211 *ctinfo += IP_CT_IS_REPLY;
212 } else {
213 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
214 *ctinfo += IP_CT_IS_REPLY;
215 }
216
e905a9ed
YH
217 /* Update skb to refer to this connection */
218 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
219 skb->nfctinfo = *ctinfo;
220 return -NF_ACCEPT;
9fb9cbb1
YK
221}
222
223/* Small and modified version of icmp_rcv */
224static int
225icmp_error(struct sk_buff *skb, unsigned int dataoff,
226 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
227{
228 struct icmphdr _ih, *icmph;
229
230 /* Not enough header? */
231 icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
232 if (icmph == NULL) {
233 if (LOG_INVALID(IPPROTO_ICMP))
234 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
235 "nf_ct_icmp: short packet ");
236 return -NF_ACCEPT;
237 }
238
239 /* See ip_conntrack_proto_tcp.c */
39a27a35 240 if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
96f6bf82 241 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
9fb9cbb1
YK
242 if (LOG_INVALID(IPPROTO_ICMP))
243 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
244 "nf_ct_icmp: bad HW ICMP checksum ");
245 return -NF_ACCEPT;
9fb9cbb1
YK
246 }
247
9fb9cbb1
YK
248 /*
249 * 18 is the highest 'known' ICMP type. Anything else is a mystery
250 *
251 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
252 * discarded.
253 */
254 if (icmph->type > NR_ICMP_TYPES) {
255 if (LOG_INVALID(IPPROTO_ICMP))
256 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
257 "nf_ct_icmp: invalid ICMP type ");
258 return -NF_ACCEPT;
259 }
260
261 /* Need to track icmp error message? */
262 if (icmph->type != ICMP_DEST_UNREACH
263 && icmph->type != ICMP_SOURCE_QUENCH
264 && icmph->type != ICMP_TIME_EXCEEDED
265 && icmph->type != ICMP_PARAMETERPROB
266 && icmph->type != ICMP_REDIRECT)
267 return NF_ACCEPT;
268
269 return icmp_error_message(skb, ctinfo, hooknum);
270}
271
c1d10adb
PNA
272#if defined(CONFIG_NF_CT_NETLINK) || \
273 defined(CONFIG_NF_CT_NETLINK_MODULE)
274
275#include <linux/netfilter/nfnetlink.h>
276#include <linux/netfilter/nfnetlink_conntrack.h>
277
278static int icmp_tuple_to_nfattr(struct sk_buff *skb,
279 const struct nf_conntrack_tuple *t)
280{
281 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
282 &t->src.u.icmp.id);
283 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
284 &t->dst.u.icmp.type);
285 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
286 &t->dst.u.icmp.code);
287
288 return 0;
289
290nfattr_failure:
291 return -1;
292}
293
294static const size_t cta_min_proto[CTA_PROTO_MAX] = {
295 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
296 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
297 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
298};
299
300static int icmp_nfattr_to_tuple(struct nfattr *tb[],
301 struct nf_conntrack_tuple *tuple)
302{
303 if (!tb[CTA_PROTO_ICMP_TYPE-1]
304 || !tb[CTA_PROTO_ICMP_CODE-1]
305 || !tb[CTA_PROTO_ICMP_ID-1])
306 return -EINVAL;
307
308 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
309 return -EINVAL;
310
e905a9ed 311 tuple->dst.u.icmp.type =
c1d10adb
PNA
312 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
313 tuple->dst.u.icmp.code =
314 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
315 tuple->src.u.icmp.id =
bff9a89b 316 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
c1d10adb
PNA
317
318 if (tuple->dst.u.icmp.type >= sizeof(invmap)
319 || !invmap[tuple->dst.u.icmp.type])
320 return -EINVAL;
321
322 return 0;
323}
324#endif
325
933a41e7
PM
326#ifdef CONFIG_SYSCTL
327static struct ctl_table_header *icmp_sysctl_header;
328static struct ctl_table icmp_sysctl_table[] = {
329 {
330 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
331 .procname = "nf_conntrack_icmp_timeout",
332 .data = &nf_ct_icmp_timeout,
333 .maxlen = sizeof(unsigned int),
334 .mode = 0644,
335 .proc_handler = &proc_dointvec_jiffies,
336 },
e905a9ed 337 {
933a41e7
PM
338 .ctl_name = 0
339 }
340};
a999e683
PM
341#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
342static struct ctl_table icmp_compat_sysctl_table[] = {
343 {
344 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
345 .procname = "ip_conntrack_icmp_timeout",
346 .data = &nf_ct_icmp_timeout,
347 .maxlen = sizeof(unsigned int),
348 .mode = 0644,
349 .proc_handler = &proc_dointvec_jiffies,
350 },
e905a9ed 351 {
a999e683
PM
352 .ctl_name = 0
353 }
354};
355#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
356#endif /* CONFIG_SYSCTL */
357
605dcad6 358struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
9fb9cbb1 359{
9fb9cbb1 360 .l3proto = PF_INET,
605dcad6 361 .l4proto = IPPROTO_ICMP,
9fb9cbb1
YK
362 .name = "icmp",
363 .pkt_to_tuple = icmp_pkt_to_tuple,
364 .invert_tuple = icmp_invert_tuple,
365 .print_tuple = icmp_print_tuple,
366 .print_conntrack = icmp_print_conntrack,
367 .packet = icmp_packet,
368 .new = icmp_new,
369 .error = icmp_error,
370 .destroy = NULL,
c1d10adb
PNA
371 .me = NULL,
372#if defined(CONFIG_NF_CT_NETLINK) || \
373 defined(CONFIG_NF_CT_NETLINK_MODULE)
374 .tuple_to_nfattr = icmp_tuple_to_nfattr,
375 .nfattr_to_tuple = icmp_nfattr_to_tuple,
376#endif
933a41e7
PM
377#ifdef CONFIG_SYSCTL
378 .ctl_table_header = &icmp_sysctl_header,
379 .ctl_table = icmp_sysctl_table,
a999e683
PM
380#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
381 .ctl_compat_table = icmp_compat_sysctl_table,
382#endif
933a41e7 383#endif
9fb9cbb1 384};
13b18339 385EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);
This page took 0.187844 seconds and 5 git commands to generate.