netfilter: conntrack: simplify event caching system
[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
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/netfilter.h>
12 #include <linux/in.h>
13 #include <linux/icmp.h>
14 #include <linux/seq_file.h>
15 #include <net/ip.h>
16 #include <net/checksum.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/netfilter/nf_conntrack_tuple.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_core.h>
21 #include <net/netfilter/nf_log.h>
22
23 static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
24
25 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
26                               struct nf_conntrack_tuple *tuple)
27 {
28         const struct icmphdr *hp;
29         struct icmphdr _hdr;
30
31         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
32         if (hp == NULL)
33                 return false;
34
35         tuple->dst.u.icmp.type = hp->type;
36         tuple->src.u.icmp.id = hp->un.echo.id;
37         tuple->dst.u.icmp.code = hp->code;
38
39         return true;
40 }
41
42 /* Add 1; spaces filled with 0. */
43 static const u_int8_t invmap[] = {
44         [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
45         [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
46         [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
47         [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
48         [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
49         [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
50         [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
51         [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
52 };
53
54 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
55                               const struct nf_conntrack_tuple *orig)
56 {
57         if (orig->dst.u.icmp.type >= sizeof(invmap)
58             || !invmap[orig->dst.u.icmp.type])
59                 return false;
60
61         tuple->src.u.icmp.id = orig->src.u.icmp.id;
62         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
63         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
64         return true;
65 }
66
67 /* Print out the per-protocol part of the tuple. */
68 static int icmp_print_tuple(struct seq_file *s,
69                             const struct nf_conntrack_tuple *tuple)
70 {
71         return seq_printf(s, "type=%u code=%u id=%u ",
72                           tuple->dst.u.icmp.type,
73                           tuple->dst.u.icmp.code,
74                           ntohs(tuple->src.u.icmp.id));
75 }
76
77 /* Returns verdict for packet, or -1 for invalid. */
78 static int icmp_packet(struct nf_conn *ct,
79                        const struct sk_buff *skb,
80                        unsigned int dataoff,
81                        enum ip_conntrack_info ctinfo,
82                        u_int8_t pf,
83                        unsigned int hooknum)
84 {
85         /* Try to delete connection immediately after all replies:
86            won't actually vanish as we still have skb, and del_timer
87            means this will only run once even if count hits zero twice
88            (theoretically possible with SMP) */
89         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
90                 if (atomic_dec_and_test(&ct->proto.icmp.count))
91                         nf_ct_kill_acct(ct, ctinfo, skb);
92         } else {
93                 atomic_inc(&ct->proto.icmp.count);
94                 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
95         }
96
97         return NF_ACCEPT;
98 }
99
100 /* Called when a new connection for this protocol found. */
101 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
102                      unsigned int dataoff)
103 {
104         static const u_int8_t valid_new[] = {
105                 [ICMP_ECHO] = 1,
106                 [ICMP_TIMESTAMP] = 1,
107                 [ICMP_INFO_REQUEST] = 1,
108                 [ICMP_ADDRESS] = 1
109         };
110
111         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
112             || !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
113                 /* Can't create a new ICMP `conn' with this. */
114                 pr_debug("icmp: can't create new conn with type %u\n",
115                          ct->tuplehash[0].tuple.dst.u.icmp.type);
116                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
117                 return false;
118         }
119         atomic_set(&ct->proto.icmp.count, 0);
120         return true;
121 }
122
123 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
124 static int
125 icmp_error_message(struct net *net, struct sk_buff *skb,
126                  enum ip_conntrack_info *ctinfo,
127                  unsigned int hooknum)
128 {
129         struct nf_conntrack_tuple innertuple, origtuple;
130         const struct nf_conntrack_l4proto *innerproto;
131         const struct nf_conntrack_tuple_hash *h;
132
133         NF_CT_ASSERT(skb->nfct == NULL);
134
135         /* Are they talking about one of our connections? */
136         if (!nf_ct_get_tuplepr(skb,
137                                skb_network_offset(skb) + ip_hdrlen(skb)
138                                                        + sizeof(struct icmphdr),
139                                PF_INET, &origtuple)) {
140                 pr_debug("icmp_error_message: failed to get tuple\n");
141                 return -NF_ACCEPT;
142         }
143
144         /* rcu_read_lock()ed by nf_hook_slow */
145         innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
146
147         /* Ordinarily, we'd expect the inverted tupleproto, but it's
148            been preserved inside the ICMP. */
149         if (!nf_ct_invert_tuple(&innertuple, &origtuple,
150                                 &nf_conntrack_l3proto_ipv4, innerproto)) {
151                 pr_debug("icmp_error_message: no match\n");
152                 return -NF_ACCEPT;
153         }
154
155         *ctinfo = IP_CT_RELATED;
156
157         h = nf_conntrack_find_get(net, &innertuple);
158         if (!h) {
159                 pr_debug("icmp_error_message: no match\n");
160                 return -NF_ACCEPT;
161         }
162
163         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
164                 *ctinfo += IP_CT_IS_REPLY;
165
166         /* Update skb to refer to this connection */
167         skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
168         skb->nfctinfo = *ctinfo;
169         return -NF_ACCEPT;
170 }
171
172 /* Small and modified version of icmp_rcv */
173 static int
174 icmp_error(struct net *net, struct sk_buff *skb, unsigned int dataoff,
175            enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
176 {
177         const struct icmphdr *icmph;
178         struct icmphdr _ih;
179
180         /* Not enough header? */
181         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
182         if (icmph == NULL) {
183                 if (LOG_INVALID(net, IPPROTO_ICMP))
184                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
185                                       "nf_ct_icmp: short packet ");
186                 return -NF_ACCEPT;
187         }
188
189         /* See ip_conntrack_proto_tcp.c */
190         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
191             nf_ip_checksum(skb, hooknum, dataoff, 0)) {
192                 if (LOG_INVALID(net, IPPROTO_ICMP))
193                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
194                                       "nf_ct_icmp: bad HW ICMP checksum ");
195                 return -NF_ACCEPT;
196         }
197
198         /*
199          *      18 is the highest 'known' ICMP type. Anything else is a mystery
200          *
201          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
202          *                discarded.
203          */
204         if (icmph->type > NR_ICMP_TYPES) {
205                 if (LOG_INVALID(net, IPPROTO_ICMP))
206                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
207                                       "nf_ct_icmp: invalid ICMP type ");
208                 return -NF_ACCEPT;
209         }
210
211         /* Need to track icmp error message? */
212         if (icmph->type != ICMP_DEST_UNREACH
213             && icmph->type != ICMP_SOURCE_QUENCH
214             && icmph->type != ICMP_TIME_EXCEEDED
215             && icmph->type != ICMP_PARAMETERPROB
216             && icmph->type != ICMP_REDIRECT)
217                 return NF_ACCEPT;
218
219         return icmp_error_message(net, skb, ctinfo, hooknum);
220 }
221
222 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
223
224 #include <linux/netfilter/nfnetlink.h>
225 #include <linux/netfilter/nfnetlink_conntrack.h>
226
227 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
228                                 const struct nf_conntrack_tuple *t)
229 {
230         NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
231         NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
232         NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
233
234         return 0;
235
236 nla_put_failure:
237         return -1;
238 }
239
240 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
241         [CTA_PROTO_ICMP_TYPE]   = { .type = NLA_U8 },
242         [CTA_PROTO_ICMP_CODE]   = { .type = NLA_U8 },
243         [CTA_PROTO_ICMP_ID]     = { .type = NLA_U16 },
244 };
245
246 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
247                                 struct nf_conntrack_tuple *tuple)
248 {
249         if (!tb[CTA_PROTO_ICMP_TYPE]
250             || !tb[CTA_PROTO_ICMP_CODE]
251             || !tb[CTA_PROTO_ICMP_ID])
252                 return -EINVAL;
253
254         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
255         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
256         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
257
258         if (tuple->dst.u.icmp.type >= sizeof(invmap)
259             || !invmap[tuple->dst.u.icmp.type])
260                 return -EINVAL;
261
262         return 0;
263 }
264
265 static int icmp_nlattr_tuple_size(void)
266 {
267         return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
268 }
269 #endif
270
271 #ifdef CONFIG_SYSCTL
272 static struct ctl_table_header *icmp_sysctl_header;
273 static struct ctl_table icmp_sysctl_table[] = {
274         {
275                 .procname       = "nf_conntrack_icmp_timeout",
276                 .data           = &nf_ct_icmp_timeout,
277                 .maxlen         = sizeof(unsigned int),
278                 .mode           = 0644,
279                 .proc_handler   = proc_dointvec_jiffies,
280         },
281         {
282                 .ctl_name = 0
283         }
284 };
285 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
286 static struct ctl_table icmp_compat_sysctl_table[] = {
287         {
288                 .procname       = "ip_conntrack_icmp_timeout",
289                 .data           = &nf_ct_icmp_timeout,
290                 .maxlen         = sizeof(unsigned int),
291                 .mode           = 0644,
292                 .proc_handler   = proc_dointvec_jiffies,
293         },
294         {
295                 .ctl_name = 0
296         }
297 };
298 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
299 #endif /* CONFIG_SYSCTL */
300
301 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
302 {
303         .l3proto                = PF_INET,
304         .l4proto                = IPPROTO_ICMP,
305         .name                   = "icmp",
306         .pkt_to_tuple           = icmp_pkt_to_tuple,
307         .invert_tuple           = icmp_invert_tuple,
308         .print_tuple            = icmp_print_tuple,
309         .packet                 = icmp_packet,
310         .new                    = icmp_new,
311         .error                  = icmp_error,
312         .destroy                = NULL,
313         .me                     = NULL,
314 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
315         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
316         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
317         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
318         .nla_policy             = icmp_nla_policy,
319 #endif
320 #ifdef CONFIG_SYSCTL
321         .ctl_table_header       = &icmp_sysctl_header,
322         .ctl_table              = icmp_sysctl_table,
323 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
324         .ctl_compat_table       = icmp_compat_sysctl_table,
325 #endif
326 #endif
327 };