blob: 0fe8fb0466ef2767741232f78305c69f26bc0f4a [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
9#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#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>
Martin Josefsson605dcad2006-11-29 02:35:06 +010019#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#include <net/netfilter/nf_conntrack_core.h>
21
Patrick McHardy933a41e2006-11-29 02:35:18 +010022static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024static int icmp_pkt_to_tuple(const struct sk_buff *skb,
25 unsigned int dataoff,
26 struct nf_conntrack_tuple *tuple)
27{
28 struct icmphdr _hdr, *hp;
29
30 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
31 if (hp == NULL)
32 return 0;
33
34 tuple->dst.u.icmp.type = hp->type;
35 tuple->src.u.icmp.id = hp->un.echo.id;
36 tuple->dst.u.icmp.code = hp->code;
37
38 return 1;
39}
40
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080041/* Add 1; spaces filled with 0. */
42static const u_int8_t invmap[] = {
43 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
44 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
45 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
46 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
47 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
48 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
49 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
50 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
51};
52
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
54 const struct nf_conntrack_tuple *orig)
55{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056 if (orig->dst.u.icmp.type >= sizeof(invmap)
57 || !invmap[orig->dst.u.icmp.type])
58 return 0;
59
60 tuple->src.u.icmp.id = orig->src.u.icmp.id;
61 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
62 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
63 return 1;
64}
65
66/* Print out the per-protocol part of the tuple. */
67static int icmp_print_tuple(struct seq_file *s,
68 const struct nf_conntrack_tuple *tuple)
69{
70 return seq_printf(s, "type=%u code=%u id=%u ",
71 tuple->dst.u.icmp.type,
72 tuple->dst.u.icmp.code,
73 ntohs(tuple->src.u.icmp.id));
74}
75
76/* Print out the private part of the conntrack. */
77static int icmp_print_conntrack(struct seq_file *s,
78 const struct nf_conn *conntrack)
79{
80 return 0;
81}
82
83/* Returns verdict for packet, or -1 for invalid. */
84static int icmp_packet(struct nf_conn *ct,
85 const struct sk_buff *skb,
86 unsigned int dataoff,
87 enum ip_conntrack_info ctinfo,
88 int pf,
89 unsigned int hooknum)
90{
91 /* Try to delete connection immediately after all replies:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090092 won't actually vanish as we still have skb, and del_timer
93 means this will only run once even if count hits zero twice
94 (theoretically possible with SMP) */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
96 if (atomic_dec_and_test(&ct->proto.icmp.count)
97 && del_timer(&ct->timeout))
98 ct->timeout.function((unsigned long)ct);
99 } else {
100 atomic_inc(&ct->proto.icmp.count);
101 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
102 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
103 }
104
105 return NF_ACCEPT;
106}
107
108/* Called when a new connection for this protocol found. */
109static int icmp_new(struct nf_conn *conntrack,
110 const struct sk_buff *skb, unsigned int dataoff)
111{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800112 static const u_int8_t valid_new[] = {
113 [ICMP_ECHO] = 1,
114 [ICMP_TIMESTAMP] = 1,
115 [ICMP_INFO_REQUEST] = 1,
116 [ICMP_ADDRESS] = 1
117 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800118
119 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
120 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
121 /* Can't create a new ICMP `conn' with this. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700122 pr_debug("icmp: can't create new conn with type %u\n",
123 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
125 return 0;
126 }
127 atomic_set(&conntrack->proto.icmp.count, 0);
128 return 1;
129}
130
131extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
132/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
133static int
134icmp_error_message(struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900135 enum ip_conntrack_info *ctinfo,
136 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800137{
138 struct nf_conntrack_tuple innertuple, origtuple;
139 struct {
140 struct icmphdr icmp;
141 struct iphdr ip;
142 } _in, *inside;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100143 struct nf_conntrack_l4proto *innerproto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144 struct nf_conntrack_tuple_hash *h;
145 int dataoff;
146
147 NF_CT_ASSERT(skb->nfct == NULL);
148
149 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300150 inside = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_in), &_in);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 if (inside == NULL)
152 return -NF_ACCEPT;
153
154 /* Ignore ICMP's containing fragments (shouldn't happen) */
155 if (inside->ip.frag_off & htons(IP_OFFSET)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700156 pr_debug("icmp_error_message: fragment of proto %u\n",
157 inside->ip.protocol);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158 return -NF_ACCEPT;
159 }
160
Patrick McHardy923f4902007-02-12 11:12:57 -0800161 /* rcu_read_lock()ed by nf_hook_slow */
Martin Josefsson605dcad2006-11-29 02:35:06 +0100162 innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
Patrick McHardy923f4902007-02-12 11:12:57 -0800163
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300164 dataoff = ip_hdrlen(skb) + sizeof(inside->icmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800165 /* Are they talking about one of our connections? */
166 if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET,
167 inside->ip.protocol, &origtuple,
168 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700169 pr_debug("icmp_error_message: ! get_tuple p=%u",
170 inside->ip.protocol);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171 return -NF_ACCEPT;
172 }
173
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900174 /* Ordinarily, we'd expect the inverted tupleproto, but it's
175 been preserved inside the ICMP. */
176 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700178 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800179 return -NF_ACCEPT;
180 }
181
182 *ctinfo = IP_CT_RELATED;
183
Patrick McHardy330f7db2007-07-07 22:28:42 -0700184 h = nf_conntrack_find_get(&innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185 if (!h) {
186 /* Locally generated ICMPs will match inverted if they
187 haven't been SNAT'ed yet */
188 /* FIXME: NAT code has to handle half-done double NAT --RR */
189 if (hooknum == NF_IP_LOCAL_OUT)
Patrick McHardy330f7db2007-07-07 22:28:42 -0700190 h = nf_conntrack_find_get(&origtuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191
192 if (!h) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700193 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 return -NF_ACCEPT;
195 }
196
197 /* Reverse direction from that found */
198 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
199 *ctinfo += IP_CT_IS_REPLY;
200 } else {
201 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
202 *ctinfo += IP_CT_IS_REPLY;
203 }
204
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900205 /* Update skb to refer to this connection */
206 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
207 skb->nfctinfo = *ctinfo;
208 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209}
210
211/* Small and modified version of icmp_rcv */
212static int
213icmp_error(struct sk_buff *skb, unsigned int dataoff,
214 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
215{
216 struct icmphdr _ih, *icmph;
217
218 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300219 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800220 if (icmph == NULL) {
221 if (LOG_INVALID(IPPROTO_ICMP))
222 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
223 "nf_ct_icmp: short packet ");
224 return -NF_ACCEPT;
225 }
226
227 /* See ip_conntrack_proto_tcp.c */
Patrick McHardy39a27a32006-05-29 18:23:54 -0700228 if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700229 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800230 if (LOG_INVALID(IPPROTO_ICMP))
231 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
232 "nf_ct_icmp: bad HW ICMP checksum ");
233 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800234 }
235
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800236 /*
237 * 18 is the highest 'known' ICMP type. Anything else is a mystery
238 *
239 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
240 * discarded.
241 */
242 if (icmph->type > NR_ICMP_TYPES) {
243 if (LOG_INVALID(IPPROTO_ICMP))
244 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
245 "nf_ct_icmp: invalid ICMP type ");
246 return -NF_ACCEPT;
247 }
248
249 /* Need to track icmp error message? */
250 if (icmph->type != ICMP_DEST_UNREACH
251 && icmph->type != ICMP_SOURCE_QUENCH
252 && icmph->type != ICMP_TIME_EXCEEDED
253 && icmph->type != ICMP_PARAMETERPROB
254 && icmph->type != ICMP_REDIRECT)
255 return NF_ACCEPT;
256
257 return icmp_error_message(skb, ctinfo, hooknum);
258}
259
Patrick McHardye281db5c2007-03-04 15:57:25 -0800260#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800261
262#include <linux/netfilter/nfnetlink.h>
263#include <linux/netfilter/nfnetlink_conntrack.h>
264
265static int icmp_tuple_to_nfattr(struct sk_buff *skb,
266 const struct nf_conntrack_tuple *t)
267{
268 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
269 &t->src.u.icmp.id);
270 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
271 &t->dst.u.icmp.type);
272 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
273 &t->dst.u.icmp.code);
274
275 return 0;
276
277nfattr_failure:
278 return -1;
279}
280
281static const size_t cta_min_proto[CTA_PROTO_MAX] = {
282 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
283 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
284 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
285};
286
287static int icmp_nfattr_to_tuple(struct nfattr *tb[],
288 struct nf_conntrack_tuple *tuple)
289{
290 if (!tb[CTA_PROTO_ICMP_TYPE-1]
291 || !tb[CTA_PROTO_ICMP_CODE-1]
292 || !tb[CTA_PROTO_ICMP_ID-1])
293 return -EINVAL;
294
295 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
296 return -EINVAL;
297
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900298 tuple->dst.u.icmp.type =
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800299 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
300 tuple->dst.u.icmp.code =
301 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
302 tuple->src.u.icmp.id =
Patrick McHardybff9a892006-12-02 22:05:08 -0800303 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800304
305 if (tuple->dst.u.icmp.type >= sizeof(invmap)
306 || !invmap[tuple->dst.u.icmp.type])
307 return -EINVAL;
308
309 return 0;
310}
311#endif
312
Patrick McHardy933a41e2006-11-29 02:35:18 +0100313#ifdef CONFIG_SYSCTL
314static struct ctl_table_header *icmp_sysctl_header;
315static struct ctl_table icmp_sysctl_table[] = {
316 {
317 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
318 .procname = "nf_conntrack_icmp_timeout",
319 .data = &nf_ct_icmp_timeout,
320 .maxlen = sizeof(unsigned int),
321 .mode = 0644,
322 .proc_handler = &proc_dointvec_jiffies,
323 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100325 .ctl_name = 0
326 }
327};
Patrick McHardya999e682006-11-29 02:35:20 +0100328#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
329static struct ctl_table icmp_compat_sysctl_table[] = {
330 {
331 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
332 .procname = "ip_conntrack_icmp_timeout",
333 .data = &nf_ct_icmp_timeout,
334 .maxlen = sizeof(unsigned int),
335 .mode = 0644,
336 .proc_handler = &proc_dointvec_jiffies,
337 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900338 {
Patrick McHardya999e682006-11-29 02:35:20 +0100339 .ctl_name = 0
340 }
341};
342#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100343#endif /* CONFIG_SYSCTL */
344
Martin Josefsson605dcad2006-11-29 02:35:06 +0100345struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800346{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800347 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100348 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349 .name = "icmp",
350 .pkt_to_tuple = icmp_pkt_to_tuple,
351 .invert_tuple = icmp_invert_tuple,
352 .print_tuple = icmp_print_tuple,
353 .print_conntrack = icmp_print_conntrack,
354 .packet = icmp_packet,
355 .new = icmp_new,
356 .error = icmp_error,
357 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800358 .me = NULL,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800359#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800360 .tuple_to_nfattr = icmp_tuple_to_nfattr,
361 .nfattr_to_tuple = icmp_nfattr_to_tuple,
362#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100363#ifdef CONFIG_SYSCTL
364 .ctl_table_header = &icmp_sysctl_header,
365 .ctl_table = icmp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100366#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
367 .ctl_compat_table = icmp_compat_sysctl_table,
368#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100369#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800370};
Patrick McHardy13b18332006-12-02 22:11:25 -0800371EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);