blob: 23b2c2ee869a85c4f92a5a45bce4f731e4815e13 [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>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080021#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080022
Patrick McHardy71320af2009-01-12 00:06:07 +000023static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024
Jan Engelhardt09f263c2008-04-14 11:15:53 +020025static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
26 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027{
Jan Engelhardt7cc38642008-01-31 04:53:05 -080028 const struct icmphdr *hp;
29 struct icmphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030
31 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
32 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +020033 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034
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
Jan Engelhardt09f263c2008-04-14 11:15:53 +020039 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040}
41
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080042/* Add 1; spaces filled with 0. */
43static 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
Jan Engelhardt09f263c2008-04-14 11:15:53 +020054static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080056{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 if (orig->dst.u.icmp.type >= sizeof(invmap)
58 || !invmap[orig->dst.u.icmp.type])
Jan Engelhardt09f263c2008-04-14 11:15:53 +020059 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060
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;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020064 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080065}
66
67/* Print out the per-protocol part of the tuple. */
68static 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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080077/* Returns verdict for packet, or -1 for invalid. */
78static int icmp_packet(struct nf_conn *ct,
79 const struct sk_buff *skb,
80 unsigned int dataoff,
81 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020082 u_int8_t pf,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083 unsigned int hooknum)
84{
85 /* Try to delete connection immediately after all replies:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090086 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) */
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
Patrick McHardy51091762008-06-09 15:59:06 -070090 if (atomic_dec_and_test(&ct->proto.icmp.count))
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -070091 nf_ct_kill_acct(ct, ctinfo, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092 } else {
93 atomic_inc(&ct->proto.icmp.count);
Alexey Dobriyana71996f2008-10-08 11:35:07 +020094 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
96 }
97
98 return NF_ACCEPT;
99}
100
101/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200102static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
103 unsigned int dataoff)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800105 static const u_int8_t valid_new[] = {
106 [ICMP_ECHO] = 1,
107 [ICMP_TIMESTAMP] = 1,
108 [ICMP_INFO_REQUEST] = 1,
109 [ICMP_ADDRESS] = 1
110 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800111
Patrick McHardyc88130b2008-01-31 04:42:11 -0800112 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
113 || !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800114 /* Can't create a new ICMP `conn' with this. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700115 pr_debug("icmp: can't create new conn with type %u\n",
Patrick McHardyc88130b2008-01-31 04:42:11 -0800116 ct->tuplehash[0].tuple.dst.u.icmp.type);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200117 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200118 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119 }
Patrick McHardyc88130b2008-01-31 04:42:11 -0800120 atomic_set(&ct->proto.icmp.count, 0);
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200121 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122}
123
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
125static int
Alexey Dobriyan74c51a12008-10-08 11:35:05 +0200126icmp_error_message(struct net *net, struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900127 enum ip_conntrack_info *ctinfo,
128 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800129{
130 struct nf_conntrack_tuple innertuple, origtuple;
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800131 const struct nf_conntrack_l4proto *innerproto;
132 const struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133
134 NF_CT_ASSERT(skb->nfct == NULL);
135
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700136 /* Are they talking about one of our connections? */
137 if (!nf_ct_get_tuplepr(skb,
138 skb_network_offset(skb) + ip_hdrlen(skb)
139 + sizeof(struct icmphdr),
140 PF_INET, &origtuple)) {
141 pr_debug("icmp_error_message: failed to get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800142 return -NF_ACCEPT;
143 }
144
Patrick McHardy923f4902007-02-12 11:12:57 -0800145 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700146 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900148 /* Ordinarily, we'd expect the inverted tupleproto, but it's
149 been preserved inside the ICMP. */
150 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700152 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800153 return -NF_ACCEPT;
154 }
155
156 *ctinfo = IP_CT_RELATED;
157
Alexey Dobriyan74c51a12008-10-08 11:35:05 +0200158 h = nf_conntrack_find_get(net, &innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800159 if (!h) {
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700160 pr_debug("icmp_error_message: no match\n");
161 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162 }
163
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700164 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
165 *ctinfo += IP_CT_IS_REPLY;
166
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900167 /* Update skb to refer to this connection */
168 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
169 skb->nfctinfo = *ctinfo;
170 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800171}
172
173/* Small and modified version of icmp_rcv */
174static int
Alexey Dobriyan74c51a12008-10-08 11:35:05 +0200175icmp_error(struct net *net, struct sk_buff *skb, unsigned int dataoff,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200176 enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800177{
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800178 const struct icmphdr *icmph;
179 struct icmphdr _ih;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800180
181 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300182 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183 if (icmph == NULL) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200184 if (LOG_INVALID(net, IPPROTO_ICMP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
186 "nf_ct_icmp: short packet ");
187 return -NF_ACCEPT;
188 }
189
190 /* See ip_conntrack_proto_tcp.c */
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200191 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700192 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200193 if (LOG_INVALID(net, IPPROTO_ICMP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800194 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
195 "nf_ct_icmp: bad HW ICMP checksum ");
196 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800197 }
198
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800199 /*
200 * 18 is the highest 'known' ICMP type. Anything else is a mystery
201 *
202 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
203 * discarded.
204 */
205 if (icmph->type > NR_ICMP_TYPES) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200206 if (LOG_INVALID(net, IPPROTO_ICMP))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
208 "nf_ct_icmp: invalid ICMP type ");
209 return -NF_ACCEPT;
210 }
211
212 /* Need to track icmp error message? */
213 if (icmph->type != ICMP_DEST_UNREACH
214 && icmph->type != ICMP_SOURCE_QUENCH
215 && icmph->type != ICMP_TIME_EXCEEDED
216 && icmph->type != ICMP_PARAMETERPROB
217 && icmph->type != ICMP_REDIRECT)
218 return NF_ACCEPT;
219
Alexey Dobriyan74c51a12008-10-08 11:35:05 +0200220 return icmp_error_message(net, skb, ctinfo, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800221}
222
Patrick McHardye281db5c2007-03-04 15:57:25 -0800223#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800224
225#include <linux/netfilter/nfnetlink.h>
226#include <linux/netfilter/nfnetlink_conntrack.h>
227
Patrick McHardyfdf70832007-09-28 14:37:41 -0700228static int icmp_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800229 const struct nf_conntrack_tuple *t)
230{
Patrick McHardy77236b62007-12-17 22:29:45 -0800231 NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
232 NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
233 NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800234
235 return 0;
236
Patrick McHardydf6fb862007-09-28 14:37:03 -0700237nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800238 return -1;
239}
240
Patrick McHardyf73e9242007-09-28 14:39:55 -0700241static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
242 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
243 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
244 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800245};
246
Patrick McHardyfdf70832007-09-28 14:37:41 -0700247static int icmp_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800248 struct nf_conntrack_tuple *tuple)
249{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700250 if (!tb[CTA_PROTO_ICMP_TYPE]
251 || !tb[CTA_PROTO_ICMP_CODE]
252 || !tb[CTA_PROTO_ICMP_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800253 return -EINVAL;
254
Patrick McHardy77236b62007-12-17 22:29:45 -0800255 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
256 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
257 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800258
259 if (tuple->dst.u.icmp.type >= sizeof(invmap)
260 || !invmap[tuple->dst.u.icmp.type])
261 return -EINVAL;
262
263 return 0;
264}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100265
266static int icmp_nlattr_tuple_size(void)
267{
268 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
269}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800270#endif
271
Patrick McHardy933a41e2006-11-29 02:35:18 +0100272#ifdef CONFIG_SYSCTL
273static struct ctl_table_header *icmp_sysctl_header;
274static struct ctl_table icmp_sysctl_table[] = {
275 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100276 .procname = "nf_conntrack_icmp_timeout",
277 .data = &nf_ct_icmp_timeout,
278 .maxlen = sizeof(unsigned int),
279 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800280 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100281 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900282 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100283 .ctl_name = 0
284 }
285};
Patrick McHardya999e682006-11-29 02:35:20 +0100286#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
287static struct ctl_table icmp_compat_sysctl_table[] = {
288 {
Patrick McHardya999e682006-11-29 02:35:20 +0100289 .procname = "ip_conntrack_icmp_timeout",
290 .data = &nf_ct_icmp_timeout,
291 .maxlen = sizeof(unsigned int),
292 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800293 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100294 },
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900295 {
Patrick McHardya999e682006-11-29 02:35:20 +0100296 .ctl_name = 0
297 }
298};
299#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100300#endif /* CONFIG_SYSCTL */
301
Patrick McHardy61075af2007-07-14 20:48:19 -0700302struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800303{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800304 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100305 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306 .name = "icmp",
307 .pkt_to_tuple = icmp_pkt_to_tuple,
308 .invert_tuple = icmp_invert_tuple,
309 .print_tuple = icmp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800310 .packet = icmp_packet,
311 .new = icmp_new,
312 .error = icmp_error,
313 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800314 .me = NULL,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800315#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700316 .tuple_to_nlattr = icmp_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100317 .nlattr_tuple_size = icmp_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700318 .nlattr_to_tuple = icmp_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700319 .nla_policy = icmp_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800320#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100321#ifdef CONFIG_SYSCTL
322 .ctl_table_header = &icmp_sysctl_header,
323 .ctl_table = icmp_sysctl_table,
Patrick McHardya999e682006-11-29 02:35:20 +0100324#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
325 .ctl_compat_table = icmp_compat_sysctl_table,
326#endif
Patrick McHardy933a41e2006-11-29 02:35:18 +0100327#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328};