]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/ipv4/netfilter/nf_conntrack_proto_icmp.c
Merge branch 'fixes' of git://git.infradead.org/users/vkoul/slave-dma
[karo-tx-linux.git] / net / ipv4 / netfilter / nf_conntrack_proto_icmp.c
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.
7  */
8
9 #include <linux/types.h>
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>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_core.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
22 #include <net/netfilter/nf_log.h>
23
24 static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
25
26 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
27                               struct nf_conntrack_tuple *tuple)
28 {
29         const struct icmphdr *hp;
30         struct icmphdr _hdr;
31
32         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
33         if (hp == NULL)
34                 return false;
35
36         tuple->dst.u.icmp.type = hp->type;
37         tuple->src.u.icmp.id = hp->un.echo.id;
38         tuple->dst.u.icmp.code = hp->code;
39
40         return true;
41 }
42
43 /* Add 1; spaces filled with 0. */
44 static const u_int8_t invmap[] = {
45         [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
46         [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
47         [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
48         [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
49         [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
50         [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
51         [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
52         [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
53 };
54
55 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
56                               const struct nf_conntrack_tuple *orig)
57 {
58         if (orig->dst.u.icmp.type >= sizeof(invmap) ||
59             !invmap[orig->dst.u.icmp.type])
60                 return false;
61
62         tuple->src.u.icmp.id = orig->src.u.icmp.id;
63         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
64         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
65         return true;
66 }
67
68 /* Print out the per-protocol part of the tuple. */
69 static int icmp_print_tuple(struct seq_file *s,
70                             const struct nf_conntrack_tuple *tuple)
71 {
72         return seq_printf(s, "type=%u code=%u id=%u ",
73                           tuple->dst.u.icmp.type,
74                           tuple->dst.u.icmp.code,
75                           ntohs(tuple->src.u.icmp.id));
76 }
77
78 static unsigned int *icmp_get_timeouts(struct net *net)
79 {
80         return &nf_ct_icmp_timeout;
81 }
82
83 /* Returns verdict for packet, or -1 for invalid. */
84 static int icmp_packet(struct nf_conn *ct,
85                        const struct sk_buff *skb,
86                        unsigned int dataoff,
87                        enum ip_conntrack_info ctinfo,
88                        u_int8_t pf,
89                        unsigned int hooknum,
90                        unsigned int *timeout)
91 {
92         /* Do not immediately delete the connection after the first
93            successful reply to avoid excessive conntrackd traffic
94            and also to handle correctly ICMP echo reply duplicates. */
95         nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
96
97         return NF_ACCEPT;
98 }
99
100 /* Called when a new connection for this protocol found. */
101 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
102                      unsigned int dataoff, unsigned int *timeouts)
103 {
104         static const u_int8_t valid_new[] = {
105                 [ICMP_ECHO] = 1,
106                 [ICMP_TIMESTAMP] = 1,
107                 [ICMP_INFO_REQUEST] = 1,
108                 [ICMP_ADDRESS] = 1
109         };
110
111         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
112             !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
113                 /* Can't create a new ICMP `conn' with this. */
114                 pr_debug("icmp: can't create new conn with type %u\n",
115                          ct->tuplehash[0].tuple.dst.u.icmp.type);
116                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
117                 return false;
118         }
119         return true;
120 }
121
122 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
123 static int
124 icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
125                  enum ip_conntrack_info *ctinfo,
126                  unsigned int hooknum)
127 {
128         struct nf_conntrack_tuple innertuple, origtuple;
129         const struct nf_conntrack_l4proto *innerproto;
130         const struct nf_conntrack_tuple_hash *h;
131         u16 zone = tmpl ? nf_ct_zone(tmpl) : NF_CT_DEFAULT_ZONE;
132
133         NF_CT_ASSERT(skb->nfct == NULL);
134
135         /* Are they talking about one of our connections? */
136         if (!nf_ct_get_tuplepr(skb,
137                                skb_network_offset(skb) + ip_hdrlen(skb)
138                                                        + sizeof(struct icmphdr),
139                                PF_INET, &origtuple)) {
140                 pr_debug("icmp_error_message: failed to get tuple\n");
141                 return -NF_ACCEPT;
142         }
143
144         /* rcu_read_lock()ed by nf_hook_slow */
145         innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
146
147         /* Ordinarily, we'd expect the inverted tupleproto, but it's
148            been preserved inside the ICMP. */
149         if (!nf_ct_invert_tuple(&innertuple, &origtuple,
150                                 &nf_conntrack_l3proto_ipv4, innerproto)) {
151                 pr_debug("icmp_error_message: no match\n");
152                 return -NF_ACCEPT;
153         }
154
155         *ctinfo = IP_CT_RELATED;
156
157         h = nf_conntrack_find_get(net, zone, &innertuple);
158         if (!h) {
159                 pr_debug("icmp_error_message: no match\n");
160                 return -NF_ACCEPT;
161         }
162
163         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
164                 *ctinfo += IP_CT_IS_REPLY;
165
166         /* Update skb to refer to this connection */
167         skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
168         skb->nfctinfo = *ctinfo;
169         return NF_ACCEPT;
170 }
171
172 /* Small and modified version of icmp_rcv */
173 static int
174 icmp_error(struct net *net, struct nf_conn *tmpl,
175            struct sk_buff *skb, unsigned int dataoff,
176            enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
177 {
178         const struct icmphdr *icmph;
179         struct icmphdr _ih;
180
181         /* Not enough header? */
182         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
183         if (icmph == NULL) {
184                 if (LOG_INVALID(net, IPPROTO_ICMP))
185                         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 */
191         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
192             nf_ip_checksum(skb, hooknum, dataoff, 0)) {
193                 if (LOG_INVALID(net, IPPROTO_ICMP))
194                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
195                                       "nf_ct_icmp: bad HW ICMP checksum ");
196                 return -NF_ACCEPT;
197         }
198
199         /*
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) {
206                 if (LOG_INVALID(net, IPPROTO_ICMP))
207                         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
220         return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
221 }
222
223 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
224
225 #include <linux/netfilter/nfnetlink.h>
226 #include <linux/netfilter/nfnetlink_conntrack.h>
227
228 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
229                                 const struct nf_conntrack_tuple *t)
230 {
231         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);
234
235         return 0;
236
237 nla_put_failure:
238         return -1;
239 }
240
241 static 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 },
245 };
246
247 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
248                                 struct nf_conntrack_tuple *tuple)
249 {
250         if (!tb[CTA_PROTO_ICMP_TYPE] ||
251             !tb[CTA_PROTO_ICMP_CODE] ||
252             !tb[CTA_PROTO_ICMP_ID])
253                 return -EINVAL;
254
255         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]);
258
259         if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
260             !invmap[tuple->dst.u.icmp.type])
261                 return -EINVAL;
262
263         return 0;
264 }
265
266 static int icmp_nlattr_tuple_size(void)
267 {
268         return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
269 }
270 #endif
271
272 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
273
274 #include <linux/netfilter/nfnetlink.h>
275 #include <linux/netfilter/nfnetlink_cttimeout.h>
276
277 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[], void *data)
278 {
279         unsigned int *timeout = data;
280
281         if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
282                 *timeout =
283                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
284         } else {
285                 /* Set default ICMP timeout. */
286                 *timeout = nf_ct_icmp_timeout;
287         }
288         return 0;
289 }
290
291 static int
292 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
293 {
294         const unsigned int *timeout = data;
295
296         NLA_PUT_BE32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ));
297
298         return 0;
299
300 nla_put_failure:
301         return -ENOSPC;
302 }
303
304 static const struct nla_policy
305 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
306         [CTA_TIMEOUT_ICMP_TIMEOUT]      = { .type = NLA_U32 },
307 };
308 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
309
310 #ifdef CONFIG_SYSCTL
311 static struct ctl_table_header *icmp_sysctl_header;
312 static struct ctl_table icmp_sysctl_table[] = {
313         {
314                 .procname       = "nf_conntrack_icmp_timeout",
315                 .data           = &nf_ct_icmp_timeout,
316                 .maxlen         = sizeof(unsigned int),
317                 .mode           = 0644,
318                 .proc_handler   = proc_dointvec_jiffies,
319         },
320         { }
321 };
322 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
323 static struct ctl_table icmp_compat_sysctl_table[] = {
324         {
325                 .procname       = "ip_conntrack_icmp_timeout",
326                 .data           = &nf_ct_icmp_timeout,
327                 .maxlen         = sizeof(unsigned int),
328                 .mode           = 0644,
329                 .proc_handler   = proc_dointvec_jiffies,
330         },
331         { }
332 };
333 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
334 #endif /* CONFIG_SYSCTL */
335
336 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
337 {
338         .l3proto                = PF_INET,
339         .l4proto                = IPPROTO_ICMP,
340         .name                   = "icmp",
341         .pkt_to_tuple           = icmp_pkt_to_tuple,
342         .invert_tuple           = icmp_invert_tuple,
343         .print_tuple            = icmp_print_tuple,
344         .packet                 = icmp_packet,
345         .get_timeouts           = icmp_get_timeouts,
346         .new                    = icmp_new,
347         .error                  = icmp_error,
348         .destroy                = NULL,
349         .me                     = NULL,
350 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
351         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
352         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
353         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
354         .nla_policy             = icmp_nla_policy,
355 #endif
356 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
357         .ctnl_timeout           = {
358                 .nlattr_to_obj  = icmp_timeout_nlattr_to_obj,
359                 .obj_to_nlattr  = icmp_timeout_obj_to_nlattr,
360                 .nlattr_max     = CTA_TIMEOUT_ICMP_MAX,
361                 .obj_size       = sizeof(unsigned int),
362                 .nla_policy     = icmp_timeout_nla_policy,
363         },
364 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
365 #ifdef CONFIG_SYSCTL
366         .ctl_table_header       = &icmp_sysctl_header,
367         .ctl_table              = icmp_sysctl_table,
368 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
369         .ctl_compat_table       = icmp_compat_sysctl_table,
370 #endif
371 #endif
372 };