blob: 3e2e5cdda9de313bccff38f1d236754900d5b7d2 [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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800131/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
132static int
133icmp_error_message(struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900134 enum ip_conntrack_info *ctinfo,
135 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800136{
137 struct nf_conntrack_tuple innertuple, origtuple;
Martin Josefsson605dcad2006-11-29 02:35:06 +0100138 struct nf_conntrack_l4proto *innerproto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139 struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800140
141 NF_CT_ASSERT(skb->nfct == NULL);
142
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700143 /* Are they talking about one of our connections? */
144 if (!nf_ct_get_tuplepr(skb,
145 skb_network_offset(skb) + ip_hdrlen(skb)
146 + sizeof(struct icmphdr),
147 PF_INET, &origtuple)) {
148 pr_debug("icmp_error_message: failed to get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149 return -NF_ACCEPT;
150 }
151
Patrick McHardy923f4902007-02-12 11:12:57 -0800152 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700153 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155 /* Ordinarily, we'd expect the inverted tupleproto, but it's
156 been preserved inside the ICMP. */
157 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700159 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160 return -NF_ACCEPT;
161 }
162
163 *ctinfo = IP_CT_RELATED;
164
Patrick McHardy330f7db2007-07-07 22:28:42 -0700165 h = nf_conntrack_find_get(&innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166 if (!h) {
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700167 pr_debug("icmp_error_message: no match\n");
168 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169 }
170
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700171 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
172 *ctinfo += IP_CT_IS_REPLY;
173
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900174 /* Update skb to refer to this connection */
175 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
176 skb->nfctinfo = *ctinfo;
177 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178}
179
180/* Small and modified version of icmp_rcv */
181static int
182icmp_error(struct sk_buff *skb, unsigned int dataoff,
183 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
184{
185 struct icmphdr _ih, *icmph;
186
187 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300188 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189 if (icmph == NULL) {
190 if (LOG_INVALID(IPPROTO_ICMP))
191 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
192 "nf_ct_icmp: short packet ");
193 return -NF_ACCEPT;
194 }
195
196 /* See ip_conntrack_proto_tcp.c */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800197 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700198 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199 if (LOG_INVALID(IPPROTO_ICMP))
200 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
201 "nf_ct_icmp: bad HW ICMP checksum ");
202 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 }
204
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 /*
206 * 18 is the highest 'known' ICMP type. Anything else is a mystery
207 *
208 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
209 * discarded.
210 */
211 if (icmph->type > NR_ICMP_TYPES) {
212 if (LOG_INVALID(IPPROTO_ICMP))
213 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
214 "nf_ct_icmp: invalid ICMP type ");
215 return -NF_ACCEPT;
216 }
217
218 /* Need to track icmp error message? */
219 if (icmph->type != ICMP_DEST_UNREACH
220 && icmph->type != ICMP_SOURCE_QUENCH
221 && icmph->type != ICMP_TIME_EXCEEDED
222 && icmph->type != ICMP_PARAMETERPROB
223 && icmph->type != ICMP_REDIRECT)
224 return NF_ACCEPT;
225
226 return icmp_error_message(skb, ctinfo, hooknum);
227}
228
Patrick McHardye281db5c2007-03-04 15:57:25 -0800229#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800230
231#include <linux/netfilter/nfnetlink.h>
232#include <linux/netfilter/nfnetlink_conntrack.h>
233
Patrick McHardyfdf70832007-09-28 14:37:41 -0700234static int icmp_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800235 const struct nf_conntrack_tuple *t)
236{
Patrick McHardy77236b62007-12-17 22:29:45 -0800237 NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
238 NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
239 NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800240
241 return 0;
242
Patrick McHardydf6fb862007-09-28 14:37:03 -0700243nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800244 return -1;
245}
246
Patrick McHardyf73e9242007-09-28 14:39:55 -0700247static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
248 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
249 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
250 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800251};
252
Patrick McHardyfdf70832007-09-28 14:37:41 -0700253static int icmp_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800254 struct nf_conntrack_tuple *tuple)
255{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700256 if (!tb[CTA_PROTO_ICMP_TYPE]
257 || !tb[CTA_PROTO_ICMP_CODE]
258 || !tb[CTA_PROTO_ICMP_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800259 return -EINVAL;
260
Patrick McHardy77236b62007-12-17 22:29:45 -0800261 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
262 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
263 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800264
265 if (tuple->dst.u.icmp.type >= sizeof(invmap)
266 || !invmap[tuple->dst.u.icmp.type])
267 return -EINVAL;
268
269 return 0;
270}
271#endif
272
Patrick McHardy933a41e2006-11-29 02:35:18 +0100273#ifdef CONFIG_SYSCTL
274static struct ctl_table_header *icmp_sysctl_header;
275static struct ctl_table icmp_sysctl_table[] = {
276 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100277 .procname = "nf_conntrack_icmp_timeout",
278 .data = &nf_ct_icmp_timeout,
279 .maxlen = sizeof(unsigned int),
280 .mode = 0644,
281 .proc_handler = &proc_dointvec_jiffies,
282 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900283 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100284 .ctl_name = 0
285 }
286};
Patrick McHardya999e682006-11-29 02:35:20 +0100287#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
288static struct ctl_table icmp_compat_sysctl_table[] = {
289 {
Patrick McHardya999e682006-11-29 02:35:20 +0100290 .procname = "ip_conntrack_icmp_timeout",
291 .data = &nf_ct_icmp_timeout,
292 .maxlen = sizeof(unsigned int),
293 .mode = 0644,
294 .proc_handler = &proc_dointvec_jiffies,
295 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900296 {
Patrick McHardya999e682006-11-29 02:35:20 +0100297 .ctl_name = 0
298 }
299};
300#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100301#endif /* CONFIG_SYSCTL */
302
Patrick McHardy61075af2007-07-14 20:48:19 -0700303struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800305 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100306 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800307 .name = "icmp",
308 .pkt_to_tuple = icmp_pkt_to_tuple,
309 .invert_tuple = icmp_invert_tuple,
310 .print_tuple = icmp_print_tuple,
311 .print_conntrack = icmp_print_conntrack,
312 .packet = icmp_packet,
313 .new = icmp_new,
314 .error = icmp_error,
315 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800316 .me = NULL,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800317#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700318 .tuple_to_nlattr = icmp_tuple_to_nlattr,
319 .nlattr_to_tuple = icmp_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700320 .nla_policy = icmp_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800321#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100322#ifdef CONFIG_SYSCTL
323 .ctl_table_header = &icmp_sysctl_header,
324 .ctl_table = icmp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100325#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
326 .ctl_compat_table = icmp_compat_sysctl_table,
327#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100328#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800329};