Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[sfrench/cifs-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         /* Do not immediately delete the connection after the first
86            successful reply to avoid excessive conntrackd traffic
87            and also to handle correctly ICMP echo reply duplicates. */
88         nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
89
90         return NF_ACCEPT;
91 }
92
93 /* Called when a new connection for this protocol found. */
94 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
95                      unsigned int dataoff)
96 {
97         static const u_int8_t valid_new[] = {
98                 [ICMP_ECHO] = 1,
99                 [ICMP_TIMESTAMP] = 1,
100                 [ICMP_INFO_REQUEST] = 1,
101                 [ICMP_ADDRESS] = 1
102         };
103
104         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
105             !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
106                 /* Can't create a new ICMP `conn' with this. */
107                 pr_debug("icmp: can't create new conn with type %u\n",
108                          ct->tuplehash[0].tuple.dst.u.icmp.type);
109                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
110                 return false;
111         }
112         return true;
113 }
114
115 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
116 static int
117 icmp_error_message(struct net *net, struct sk_buff *skb,
118                  enum ip_conntrack_info *ctinfo,
119                  unsigned int hooknum)
120 {
121         struct nf_conntrack_tuple innertuple, origtuple;
122         const struct nf_conntrack_l4proto *innerproto;
123         const struct nf_conntrack_tuple_hash *h;
124
125         NF_CT_ASSERT(skb->nfct == NULL);
126
127         /* Are they talking about one of our connections? */
128         if (!nf_ct_get_tuplepr(skb,
129                                skb_network_offset(skb) + ip_hdrlen(skb)
130                                                        + sizeof(struct icmphdr),
131                                PF_INET, &origtuple)) {
132                 pr_debug("icmp_error_message: failed to get tuple\n");
133                 return -NF_ACCEPT;
134         }
135
136         /* rcu_read_lock()ed by nf_hook_slow */
137         innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
138
139         /* Ordinarily, we'd expect the inverted tupleproto, but it's
140            been preserved inside the ICMP. */
141         if (!nf_ct_invert_tuple(&innertuple, &origtuple,
142                                 &nf_conntrack_l3proto_ipv4, innerproto)) {
143                 pr_debug("icmp_error_message: no match\n");
144                 return -NF_ACCEPT;
145         }
146
147         *ctinfo = IP_CT_RELATED;
148
149         h = nf_conntrack_find_get(net, &innertuple);
150         if (!h) {
151                 pr_debug("icmp_error_message: no match\n");
152                 return -NF_ACCEPT;
153         }
154
155         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
156                 *ctinfo += IP_CT_IS_REPLY;
157
158         /* Update skb to refer to this connection */
159         skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
160         skb->nfctinfo = *ctinfo;
161         return -NF_ACCEPT;
162 }
163
164 /* Small and modified version of icmp_rcv */
165 static int
166 icmp_error(struct net *net, struct sk_buff *skb, unsigned int dataoff,
167            enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
168 {
169         const struct icmphdr *icmph;
170         struct icmphdr _ih;
171
172         /* Not enough header? */
173         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
174         if (icmph == NULL) {
175                 if (LOG_INVALID(net, IPPROTO_ICMP))
176                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
177                                       "nf_ct_icmp: short packet ");
178                 return -NF_ACCEPT;
179         }
180
181         /* See ip_conntrack_proto_tcp.c */
182         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
183             nf_ip_checksum(skb, hooknum, dataoff, 0)) {
184                 if (LOG_INVALID(net, IPPROTO_ICMP))
185                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
186                                       "nf_ct_icmp: bad HW ICMP checksum ");
187                 return -NF_ACCEPT;
188         }
189
190         /*
191          *      18 is the highest 'known' ICMP type. Anything else is a mystery
192          *
193          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
194          *                discarded.
195          */
196         if (icmph->type > NR_ICMP_TYPES) {
197                 if (LOG_INVALID(net, IPPROTO_ICMP))
198                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
199                                       "nf_ct_icmp: invalid ICMP type ");
200                 return -NF_ACCEPT;
201         }
202
203         /* Need to track icmp error message? */
204         if (icmph->type != ICMP_DEST_UNREACH &&
205             icmph->type != ICMP_SOURCE_QUENCH &&
206             icmph->type != ICMP_TIME_EXCEEDED &&
207             icmph->type != ICMP_PARAMETERPROB &&
208             icmph->type != ICMP_REDIRECT)
209                 return NF_ACCEPT;
210
211         return icmp_error_message(net, skb, ctinfo, hooknum);
212 }
213
214 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
215
216 #include <linux/netfilter/nfnetlink.h>
217 #include <linux/netfilter/nfnetlink_conntrack.h>
218
219 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
220                                 const struct nf_conntrack_tuple *t)
221 {
222         NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
223         NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
224         NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
225
226         return 0;
227
228 nla_put_failure:
229         return -1;
230 }
231
232 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
233         [CTA_PROTO_ICMP_TYPE]   = { .type = NLA_U8 },
234         [CTA_PROTO_ICMP_CODE]   = { .type = NLA_U8 },
235         [CTA_PROTO_ICMP_ID]     = { .type = NLA_U16 },
236 };
237
238 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
239                                 struct nf_conntrack_tuple *tuple)
240 {
241         if (!tb[CTA_PROTO_ICMP_TYPE] ||
242             !tb[CTA_PROTO_ICMP_CODE] ||
243             !tb[CTA_PROTO_ICMP_ID])
244                 return -EINVAL;
245
246         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
247         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
248         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
249
250         if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
251             !invmap[tuple->dst.u.icmp.type])
252                 return -EINVAL;
253
254         return 0;
255 }
256
257 static int icmp_nlattr_tuple_size(void)
258 {
259         return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
260 }
261 #endif
262
263 #ifdef CONFIG_SYSCTL
264 static struct ctl_table_header *icmp_sysctl_header;
265 static struct ctl_table icmp_sysctl_table[] = {
266         {
267                 .procname       = "nf_conntrack_icmp_timeout",
268                 .data           = &nf_ct_icmp_timeout,
269                 .maxlen         = sizeof(unsigned int),
270                 .mode           = 0644,
271                 .proc_handler   = proc_dointvec_jiffies,
272         },
273         { }
274 };
275 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
276 static struct ctl_table icmp_compat_sysctl_table[] = {
277         {
278                 .procname       = "ip_conntrack_icmp_timeout",
279                 .data           = &nf_ct_icmp_timeout,
280                 .maxlen         = sizeof(unsigned int),
281                 .mode           = 0644,
282                 .proc_handler   = proc_dointvec_jiffies,
283         },
284         { }
285 };
286 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
287 #endif /* CONFIG_SYSCTL */
288
289 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
290 {
291         .l3proto                = PF_INET,
292         .l4proto                = IPPROTO_ICMP,
293         .name                   = "icmp",
294         .pkt_to_tuple           = icmp_pkt_to_tuple,
295         .invert_tuple           = icmp_invert_tuple,
296         .print_tuple            = icmp_print_tuple,
297         .packet                 = icmp_packet,
298         .new                    = icmp_new,
299         .error                  = icmp_error,
300         .destroy                = NULL,
301         .me                     = NULL,
302 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
303         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
304         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
305         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
306         .nla_policy             = icmp_nla_policy,
307 #endif
308 #ifdef CONFIG_SYSCTL
309         .ctl_table_header       = &icmp_sysctl_header,
310         .ctl_table              = icmp_sysctl_table,
311 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
312         .ctl_compat_table       = icmp_compat_sysctl_table,
313 #endif
314 #endif
315 };