]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - net/ipv4/netfilter/nf_conntrack_proto_icmp.c
[NETLINK]: Add NLA_PUT_BE16/nla_get_be16()
[mirror_ubuntu-disco-kernel.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
YK
20#include <net/netfilter/nf_conntrack_core.h>
21
933a41e7 22static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
9fb9cbb1 23
9fb9cbb1
YK
24static 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
c1d10adb
PNA
41/* 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
9fb9cbb1
YK
53static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
54 const struct nf_conntrack_tuple *orig)
55{
9fb9cbb1
YK
56 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:
e905a9ed
YH
92 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) */
9fb9cbb1
YK
95 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{
c1d10adb
PNA
112 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 };
9fb9cbb1
YK
118
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. */
0d53778e
PM
122 pr_debug("icmp: can't create new conn with type %u\n",
123 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
9fb9cbb1
YK
124 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
9fb9cbb1
YK
131/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
132static int
133icmp_error_message(struct sk_buff *skb,
e905a9ed
YH
134 enum ip_conntrack_info *ctinfo,
135 unsigned int hooknum)
9fb9cbb1
YK
136{
137 struct nf_conntrack_tuple innertuple, origtuple;
605dcad6 138 struct nf_conntrack_l4proto *innerproto;
9fb9cbb1 139 struct nf_conntrack_tuple_hash *h;
9fb9cbb1
YK
140
141 NF_CT_ASSERT(skb->nfct == NULL);
142
e2a3123f
YK
143 /* 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");
9fb9cbb1
YK
149 return -NF_ACCEPT;
150 }
151
923f4902 152 /* rcu_read_lock()ed by nf_hook_slow */
e2a3123f 153 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
9fb9cbb1 154
e905a9ed
YH
155 /* Ordinarily, we'd expect the inverted tupleproto, but it's
156 been preserved inside the ICMP. */
157 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
9fb9cbb1 158 &nf_conntrack_l3proto_ipv4, innerproto)) {
0d53778e 159 pr_debug("icmp_error_message: no match\n");
9fb9cbb1
YK
160 return -NF_ACCEPT;
161 }
162
163 *ctinfo = IP_CT_RELATED;
164
330f7db5 165 h = nf_conntrack_find_get(&innertuple);
9fb9cbb1 166 if (!h) {
130e7a83
YK
167 pr_debug("icmp_error_message: no match\n");
168 return -NF_ACCEPT;
9fb9cbb1
YK
169 }
170
130e7a83
YK
171 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
172 *ctinfo += IP_CT_IS_REPLY;
173
e905a9ed
YH
174 /* 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;
9fb9cbb1
YK
178}
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? */
c9bdd4b5 188 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
9fb9cbb1
YK
189 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 */
6e23ae2a 197 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
96f6bf82 198 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
9fb9cbb1
YK
199 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;
9fb9cbb1
YK
203 }
204
9fb9cbb1
YK
205 /*
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
e281db5c 229#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
230
231#include <linux/netfilter/nfnetlink.h>
232#include <linux/netfilter/nfnetlink_conntrack.h>
233
fdf70832 234static int icmp_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
235 const struct nf_conntrack_tuple *t)
236{
df6fb868 237 NLA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
c1d10adb 238 &t->src.u.icmp.id);
df6fb868 239 NLA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
c1d10adb 240 &t->dst.u.icmp.type);
df6fb868 241 NLA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
c1d10adb
PNA
242 &t->dst.u.icmp.code);
243
244 return 0;
245
df6fb868 246nla_put_failure:
c1d10adb
PNA
247 return -1;
248}
249
f73e924c
PM
250static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
251 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
252 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
253 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
c1d10adb
PNA
254};
255
fdf70832 256static int icmp_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
257 struct nf_conntrack_tuple *tuple)
258{
df6fb868
PM
259 if (!tb[CTA_PROTO_ICMP_TYPE]
260 || !tb[CTA_PROTO_ICMP_CODE]
261 || !tb[CTA_PROTO_ICMP_ID])
c1d10adb
PNA
262 return -EINVAL;
263
e905a9ed 264 tuple->dst.u.icmp.type =
df6fb868 265 *(u_int8_t *)nla_data(tb[CTA_PROTO_ICMP_TYPE]);
c1d10adb 266 tuple->dst.u.icmp.code =
df6fb868 267 *(u_int8_t *)nla_data(tb[CTA_PROTO_ICMP_CODE]);
c1d10adb 268 tuple->src.u.icmp.id =
df6fb868 269 *(__be16 *)nla_data(tb[CTA_PROTO_ICMP_ID]);
c1d10adb
PNA
270
271 if (tuple->dst.u.icmp.type >= sizeof(invmap)
272 || !invmap[tuple->dst.u.icmp.type])
273 return -EINVAL;
274
275 return 0;
276}
277#endif
278
933a41e7
PM
279#ifdef CONFIG_SYSCTL
280static struct ctl_table_header *icmp_sysctl_header;
281static struct ctl_table icmp_sysctl_table[] = {
282 {
933a41e7
PM
283 .procname = "nf_conntrack_icmp_timeout",
284 .data = &nf_ct_icmp_timeout,
285 .maxlen = sizeof(unsigned int),
286 .mode = 0644,
287 .proc_handler = &proc_dointvec_jiffies,
288 },
e905a9ed 289 {
933a41e7
PM
290 .ctl_name = 0
291 }
292};
a999e683
PM
293#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
294static struct ctl_table icmp_compat_sysctl_table[] = {
295 {
a999e683
PM
296 .procname = "ip_conntrack_icmp_timeout",
297 .data = &nf_ct_icmp_timeout,
298 .maxlen = sizeof(unsigned int),
299 .mode = 0644,
300 .proc_handler = &proc_dointvec_jiffies,
301 },
e905a9ed 302 {
a999e683
PM
303 .ctl_name = 0
304 }
305};
306#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7
PM
307#endif /* CONFIG_SYSCTL */
308
61075af5 309struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
9fb9cbb1 310{
9fb9cbb1 311 .l3proto = PF_INET,
605dcad6 312 .l4proto = IPPROTO_ICMP,
9fb9cbb1
YK
313 .name = "icmp",
314 .pkt_to_tuple = icmp_pkt_to_tuple,
315 .invert_tuple = icmp_invert_tuple,
316 .print_tuple = icmp_print_tuple,
317 .print_conntrack = icmp_print_conntrack,
318 .packet = icmp_packet,
319 .new = icmp_new,
320 .error = icmp_error,
321 .destroy = NULL,
c1d10adb 322 .me = NULL,
e281db5c 323#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832
PM
324 .tuple_to_nlattr = icmp_tuple_to_nlattr,
325 .nlattr_to_tuple = icmp_nlattr_to_tuple,
f73e924c 326 .nla_policy = icmp_nla_policy,
c1d10adb 327#endif
933a41e7
PM
328#ifdef CONFIG_SYSCTL
329 .ctl_table_header = &icmp_sysctl_header,
330 .ctl_table = icmp_sysctl_table,
a999e683
PM
331#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
332 .ctl_compat_table = icmp_compat_sysctl_table,
333#endif
933a41e7 334#endif
9fb9cbb1 335};