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