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