netfilter: nf_conntrack: pass template to l4proto ->error() handler
[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
71320afc 23static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
9fb9cbb1 24
09f263cd
JE
25static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
26 struct nf_conntrack_tuple *tuple)
9fb9cbb1 27{
7cc3864d
JE
28 const struct icmphdr *hp;
29 struct icmphdr _hdr;
9fb9cbb1
YK
30
31 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
32 if (hp == NULL)
09f263cd 33 return false;
9fb9cbb1
YK
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
09f263cd 39 return true;
9fb9cbb1
YK
40}
41
c1d10adb
PNA
42/* 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
09f263cd
JE
54static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
9fb9cbb1 56{
3666ed1c
JP
57 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
58 !invmap[orig->dst.u.icmp.type])
09f263cd 59 return false;
9fb9cbb1
YK
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;
09f263cd 64 return true;
9fb9cbb1
YK
65}
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
9fb9cbb1
YK
77/* 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,
76108cea 82 u_int8_t pf,
9fb9cbb1
YK
83 unsigned int hooknum)
84{
f87fb666
JK
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);
9fb9cbb1
YK
89
90 return NF_ACCEPT;
91}
92
93/* Called when a new connection for this protocol found. */
09f263cd
JE
94static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
95 unsigned int dataoff)
9fb9cbb1 96{
c1d10adb
PNA
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 };
9fb9cbb1 103
3666ed1c
JP
104 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
105 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
9fb9cbb1 106 /* Can't create a new ICMP `conn' with this. */
0d53778e 107 pr_debug("icmp: can't create new conn with type %u\n",
c88130bc 108 ct->tuplehash[0].tuple.dst.u.icmp.type);
3c9fba65 109 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
09f263cd 110 return false;
9fb9cbb1 111 }
09f263cd 112 return true;
9fb9cbb1
YK
113}
114
9fb9cbb1
YK
115/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
116static int
74c51a14 117icmp_error_message(struct net *net, struct sk_buff *skb,
e905a9ed
YH
118 enum ip_conntrack_info *ctinfo,
119 unsigned int hooknum)
9fb9cbb1
YK
120{
121 struct nf_conntrack_tuple innertuple, origtuple;
7cc3864d
JE
122 const struct nf_conntrack_l4proto *innerproto;
123 const struct nf_conntrack_tuple_hash *h;
9fb9cbb1
YK
124
125 NF_CT_ASSERT(skb->nfct == NULL);
126
e2a3123f
YK
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");
9fb9cbb1
YK
133 return -NF_ACCEPT;
134 }
135
923f4902 136 /* rcu_read_lock()ed by nf_hook_slow */
e2a3123f 137 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
9fb9cbb1 138
e905a9ed
YH
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,
9fb9cbb1 142 &nf_conntrack_l3proto_ipv4, innerproto)) {
0d53778e 143 pr_debug("icmp_error_message: no match\n");
9fb9cbb1
YK
144 return -NF_ACCEPT;
145 }
146
147 *ctinfo = IP_CT_RELATED;
148
74c51a14 149 h = nf_conntrack_find_get(net, &innertuple);
9fb9cbb1 150 if (!h) {
130e7a83
YK
151 pr_debug("icmp_error_message: no match\n");
152 return -NF_ACCEPT;
9fb9cbb1
YK
153 }
154
130e7a83
YK
155 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
156 *ctinfo += IP_CT_IS_REPLY;
157
e905a9ed
YH
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;
9fb9cbb1
YK
162}
163
164/* Small and modified version of icmp_rcv */
165static int
8fea97ec
PM
166icmp_error(struct net *net, struct nf_conn *tmpl,
167 struct sk_buff *skb, unsigned int dataoff,
76108cea 168 enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
9fb9cbb1 169{
7cc3864d
JE
170 const struct icmphdr *icmph;
171 struct icmphdr _ih;
9fb9cbb1
YK
172
173 /* Not enough header? */
c9bdd4b5 174 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
9fb9cbb1 175 if (icmph == NULL) {
c2a2c7e0 176 if (LOG_INVALID(net, IPPROTO_ICMP))
9fb9cbb1
YK
177 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
178 "nf_ct_icmp: short packet ");
179 return -NF_ACCEPT;
180 }
181
182 /* See ip_conntrack_proto_tcp.c */
c04d0552 183 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
96f6bf82 184 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
c2a2c7e0 185 if (LOG_INVALID(net, IPPROTO_ICMP))
9fb9cbb1
YK
186 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
187 "nf_ct_icmp: bad HW ICMP checksum ");
188 return -NF_ACCEPT;
9fb9cbb1
YK
189 }
190
9fb9cbb1
YK
191 /*
192 * 18 is the highest 'known' ICMP type. Anything else is a mystery
193 *
194 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
195 * discarded.
196 */
197 if (icmph->type > NR_ICMP_TYPES) {
c2a2c7e0 198 if (LOG_INVALID(net, IPPROTO_ICMP))
9fb9cbb1
YK
199 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
200 "nf_ct_icmp: invalid ICMP type ");
201 return -NF_ACCEPT;
202 }
203
204 /* Need to track icmp error message? */
3666ed1c
JP
205 if (icmph->type != ICMP_DEST_UNREACH &&
206 icmph->type != ICMP_SOURCE_QUENCH &&
207 icmph->type != ICMP_TIME_EXCEEDED &&
208 icmph->type != ICMP_PARAMETERPROB &&
209 icmph->type != ICMP_REDIRECT)
9fb9cbb1
YK
210 return NF_ACCEPT;
211
74c51a14 212 return icmp_error_message(net, skb, ctinfo, hooknum);
9fb9cbb1
YK
213}
214
e281db5c 215#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
216
217#include <linux/netfilter/nfnetlink.h>
218#include <linux/netfilter/nfnetlink_conntrack.h>
219
fdf70832 220static int icmp_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
221 const struct nf_conntrack_tuple *t)
222{
77236b6e
PM
223 NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
224 NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
225 NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
c1d10adb
PNA
226
227 return 0;
228
df6fb868 229nla_put_failure:
c1d10adb
PNA
230 return -1;
231}
232
f73e924c
PM
233static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
234 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
235 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
236 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
c1d10adb
PNA
237};
238
fdf70832 239static int icmp_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
240 struct nf_conntrack_tuple *tuple)
241{
3666ed1c
JP
242 if (!tb[CTA_PROTO_ICMP_TYPE] ||
243 !tb[CTA_PROTO_ICMP_CODE] ||
244 !tb[CTA_PROTO_ICMP_ID])
c1d10adb
PNA
245 return -EINVAL;
246
77236b6e
PM
247 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
248 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
249 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
c1d10adb 250
3666ed1c
JP
251 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
252 !invmap[tuple->dst.u.icmp.type])
c1d10adb
PNA
253 return -EINVAL;
254
255 return 0;
256}
a400c30e
HE
257
258static int icmp_nlattr_tuple_size(void)
259{
260 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
261}
c1d10adb
PNA
262#endif
263
933a41e7
PM
264#ifdef CONFIG_SYSCTL
265static struct ctl_table_header *icmp_sysctl_header;
266static struct ctl_table icmp_sysctl_table[] = {
267 {
933a41e7
PM
268 .procname = "nf_conntrack_icmp_timeout",
269 .data = &nf_ct_icmp_timeout,
270 .maxlen = sizeof(unsigned int),
271 .mode = 0644,
6d9f239a 272 .proc_handler = proc_dointvec_jiffies,
933a41e7 273 },
f8572d8f 274 { }
933a41e7 275};
a999e683
PM
276#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
277static struct ctl_table icmp_compat_sysctl_table[] = {
278 {
a999e683
PM
279 .procname = "ip_conntrack_icmp_timeout",
280 .data = &nf_ct_icmp_timeout,
281 .maxlen = sizeof(unsigned int),
282 .mode = 0644,
6d9f239a 283 .proc_handler = proc_dointvec_jiffies,
a999e683 284 },
f8572d8f 285 { }
a999e683
PM
286};
287#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
288#endif /* CONFIG_SYSCTL */
289
61075af5 290struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
9fb9cbb1 291{
9fb9cbb1 292 .l3proto = PF_INET,
605dcad6 293 .l4proto = IPPROTO_ICMP,
9fb9cbb1
YK
294 .name = "icmp",
295 .pkt_to_tuple = icmp_pkt_to_tuple,
296 .invert_tuple = icmp_invert_tuple,
297 .print_tuple = icmp_print_tuple,
9fb9cbb1
YK
298 .packet = icmp_packet,
299 .new = icmp_new,
300 .error = icmp_error,
301 .destroy = NULL,
c1d10adb 302 .me = NULL,
e281db5c 303#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832 304 .tuple_to_nlattr = icmp_tuple_to_nlattr,
a400c30e 305 .nlattr_tuple_size = icmp_nlattr_tuple_size,
fdf70832 306 .nlattr_to_tuple = icmp_nlattr_to_tuple,
f73e924c 307 .nla_policy = icmp_nla_policy,
c1d10adb 308#endif
933a41e7
PM
309#ifdef CONFIG_SYSCTL
310 .ctl_table_header = &icmp_sysctl_header,
311 .ctl_table = icmp_sysctl_table,
a999e683
PM
312#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
313 .ctl_compat_table = icmp_compat_sysctl_table,
314#endif
933a41e7 315#endif
9fb9cbb1 316};
This page took 0.470552 seconds and 5 git commands to generate.