GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / netfilter / nf_conntrack_proto_icmp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <linux/in.h>
14 #include <linux/icmp.h>
15 #include <linux/seq_file.h>
16 #include <net/ip.h>
17 #include <net/checksum.h>
18 #include <linux/netfilter_ipv4.h>
19 #include <net/netfilter/nf_conntrack_tuple.h>
20 #include <net/netfilter/nf_conntrack_l4proto.h>
21 #include <net/netfilter/nf_conntrack_core.h>
22 #include <net/netfilter/nf_conntrack_timeout.h>
23 #include <net/netfilter/nf_conntrack_zones.h>
24 #include <net/netfilter/nf_log.h>
25
26 static const unsigned int nf_ct_icmp_timeout = 30*HZ;
27
28 static inline struct nf_icmp_net *icmp_pernet(struct net *net)
29 {
30         return &net->ct.nf_ct_proto.icmp;
31 }
32
33 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
34                               struct net *net, struct nf_conntrack_tuple *tuple)
35 {
36         const struct icmphdr *hp;
37         struct icmphdr _hdr;
38
39         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
40         if (hp == NULL)
41                 return false;
42
43         tuple->dst.u.icmp.type = hp->type;
44         tuple->src.u.icmp.id = hp->un.echo.id;
45         tuple->dst.u.icmp.code = hp->code;
46
47         return true;
48 }
49
50 /* Add 1; spaces filled with 0. */
51 static const u_int8_t invmap[] = {
52         [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
53         [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
54         [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
55         [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
56         [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
57         [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
58         [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
59         [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
60 };
61
62 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
63                               const struct nf_conntrack_tuple *orig)
64 {
65         if (orig->dst.u.icmp.type >= sizeof(invmap) ||
66             !invmap[orig->dst.u.icmp.type])
67                 return false;
68
69         tuple->src.u.icmp.id = orig->src.u.icmp.id;
70         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
71         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
72         return true;
73 }
74
75 static unsigned int *icmp_get_timeouts(struct net *net)
76 {
77         return &icmp_pernet(net)->timeout;
78 }
79
80 /* Returns verdict for packet, or -1 for invalid. */
81 static int icmp_packet(struct nf_conn *ct,
82                        const struct sk_buff *skb,
83                        unsigned int dataoff,
84                        enum ip_conntrack_info ctinfo)
85 {
86         /* Do not immediately delete the connection after the first
87            successful reply to avoid excessive conntrackd traffic
88            and also to handle correctly ICMP echo reply duplicates. */
89         unsigned int *timeout = nf_ct_timeout_lookup(ct);
90
91         if (!timeout)
92                 timeout = icmp_get_timeouts(nf_ct_net(ct));
93
94         nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
95
96         return NF_ACCEPT;
97 }
98
99 /* Called when a new connection for this protocol found. */
100 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
101                      unsigned int dataoff)
102 {
103         static const u_int8_t valid_new[] = {
104                 [ICMP_ECHO] = 1,
105                 [ICMP_TIMESTAMP] = 1,
106                 [ICMP_INFO_REQUEST] = 1,
107                 [ICMP_ADDRESS] = 1
108         };
109
110         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
111             !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
112                 /* Can't create a new ICMP `conn' with this. */
113                 pr_debug("icmp: can't create new conn with type %u\n",
114                          ct->tuplehash[0].tuple.dst.u.icmp.type);
115                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
116                 return false;
117         }
118         return true;
119 }
120
121 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
122 static int
123 icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
124                  unsigned int hooknum)
125 {
126         struct nf_conntrack_tuple innertuple, origtuple;
127         const struct nf_conntrack_l4proto *innerproto;
128         const struct nf_conntrack_tuple_hash *h;
129         const struct nf_conntrack_zone *zone;
130         enum ip_conntrack_info ctinfo;
131         struct nf_conntrack_zone tmp;
132
133         WARN_ON(skb_nfct(skb));
134         zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
135
136         /* 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, net, &origtuple)) {
141                 pr_debug("icmp_error_message: failed to get tuple\n");
142                 return -NF_ACCEPT;
143         }
144
145         /* rcu_read_lock()ed by nf_hook_thresh */
146         innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
147
148         /* Ordinarily, we'd expect the inverted tupleproto, but it's
149            been preserved inside the ICMP. */
150         if (!nf_ct_invert_tuple(&innertuple, &origtuple, 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         nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
168         return NF_ACCEPT;
169 }
170
171 static void icmp_error_log(const struct sk_buff *skb, struct net *net,
172                            u8 pf, const char *msg)
173 {
174         nf_l4proto_log_invalid(skb, net, pf, IPPROTO_ICMP, "%s", msg);
175 }
176
177 /* Small and modified version of icmp_rcv */
178 static int
179 icmp_error(struct net *net, struct nf_conn *tmpl,
180            struct sk_buff *skb, unsigned int dataoff,
181            u8 pf, unsigned int hooknum)
182 {
183         const struct icmphdr *icmph;
184         struct icmphdr _ih;
185
186         /* Not enough header? */
187         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
188         if (icmph == NULL) {
189                 icmp_error_log(skb, net, pf, "short packet");
190                 return -NF_ACCEPT;
191         }
192
193         /* See ip_conntrack_proto_tcp.c */
194         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
195             nf_ip_checksum(skb, hooknum, dataoff, 0)) {
196                 icmp_error_log(skb, net, pf, "bad hw icmp checksum");
197                 return -NF_ACCEPT;
198         }
199
200         /*
201          *      18 is the highest 'known' ICMP type. Anything else is a mystery
202          *
203          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
204          *                discarded.
205          */
206         if (icmph->type > NR_ICMP_TYPES) {
207                 icmp_error_log(skb, net, pf, "invalid icmp type");
208                 return -NF_ACCEPT;
209         }
210
211         /* Need to track icmp error message? */
212         if (icmph->type != ICMP_DEST_UNREACH &&
213             icmph->type != ICMP_SOURCE_QUENCH &&
214             icmph->type != ICMP_TIME_EXCEEDED &&
215             icmph->type != ICMP_PARAMETERPROB &&
216             icmph->type != ICMP_REDIRECT)
217                 return NF_ACCEPT;
218
219         return icmp_error_message(net, tmpl, skb, hooknum);
220 }
221
222 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
223
224 #include <linux/netfilter/nfnetlink.h>
225 #include <linux/netfilter/nfnetlink_conntrack.h>
226
227 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
228                                 const struct nf_conntrack_tuple *t)
229 {
230         if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
231             nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
232             nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
233                 goto nla_put_failure;
234         return 0;
235
236 nla_put_failure:
237         return -1;
238 }
239
240 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
241         [CTA_PROTO_ICMP_TYPE]   = { .type = NLA_U8 },
242         [CTA_PROTO_ICMP_CODE]   = { .type = NLA_U8 },
243         [CTA_PROTO_ICMP_ID]     = { .type = NLA_U16 },
244 };
245
246 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
247                                 struct nf_conntrack_tuple *tuple)
248 {
249         if (!tb[CTA_PROTO_ICMP_TYPE] ||
250             !tb[CTA_PROTO_ICMP_CODE] ||
251             !tb[CTA_PROTO_ICMP_ID])
252                 return -EINVAL;
253
254         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
255         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
256         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
257
258         if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
259             !invmap[tuple->dst.u.icmp.type])
260                 return -EINVAL;
261
262         return 0;
263 }
264
265 static unsigned int icmp_nlattr_tuple_size(void)
266 {
267         static unsigned int size __read_mostly;
268
269         if (!size)
270                 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
271
272         return size;
273 }
274 #endif
275
276 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
277
278 #include <linux/netfilter/nfnetlink.h>
279 #include <linux/netfilter/nfnetlink_cttimeout.h>
280
281 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
282                                       struct net *net, void *data)
283 {
284         unsigned int *timeout = data;
285         struct nf_icmp_net *in = icmp_pernet(net);
286
287         if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
288                 if (!timeout)
289                         timeout = &in->timeout;
290                 *timeout =
291                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
292         } else if (timeout) {
293                 /* Set default ICMP timeout. */
294                 *timeout = in->timeout;
295         }
296         return 0;
297 }
298
299 static int
300 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
301 {
302         const unsigned int *timeout = data;
303
304         if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
305                 goto nla_put_failure;
306         return 0;
307
308 nla_put_failure:
309         return -ENOSPC;
310 }
311
312 static const struct nla_policy
313 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
314         [CTA_TIMEOUT_ICMP_TIMEOUT]      = { .type = NLA_U32 },
315 };
316 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
317
318 #ifdef CONFIG_SYSCTL
319 static struct ctl_table icmp_sysctl_table[] = {
320         {
321                 .procname       = "nf_conntrack_icmp_timeout",
322                 .maxlen         = sizeof(unsigned int),
323                 .mode           = 0644,
324                 .proc_handler   = proc_dointvec_jiffies,
325         },
326         { }
327 };
328 #endif /* CONFIG_SYSCTL */
329
330 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
331                                      struct nf_icmp_net *in)
332 {
333 #ifdef CONFIG_SYSCTL
334         pn->ctl_table = kmemdup(icmp_sysctl_table,
335                                 sizeof(icmp_sysctl_table),
336                                 GFP_KERNEL);
337         if (!pn->ctl_table)
338                 return -ENOMEM;
339
340         pn->ctl_table[0].data = &in->timeout;
341 #endif
342         return 0;
343 }
344
345 static int icmp_init_net(struct net *net, u_int16_t proto)
346 {
347         struct nf_icmp_net *in = icmp_pernet(net);
348         struct nf_proto_net *pn = &in->pn;
349
350         in->timeout = nf_ct_icmp_timeout;
351
352         return icmp_kmemdup_sysctl_table(pn, in);
353 }
354
355 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
356 {
357         return &net->ct.nf_ct_proto.icmp.pn;
358 }
359
360 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
361 {
362         .l3proto                = PF_INET,
363         .l4proto                = IPPROTO_ICMP,
364         .pkt_to_tuple           = icmp_pkt_to_tuple,
365         .invert_tuple           = icmp_invert_tuple,
366         .packet                 = icmp_packet,
367         .new                    = icmp_new,
368         .error                  = icmp_error,
369         .destroy                = NULL,
370         .me                     = NULL,
371 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
372         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
373         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
374         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
375         .nla_policy             = icmp_nla_policy,
376 #endif
377 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
378         .ctnl_timeout           = {
379                 .nlattr_to_obj  = icmp_timeout_nlattr_to_obj,
380                 .obj_to_nlattr  = icmp_timeout_obj_to_nlattr,
381                 .nlattr_max     = CTA_TIMEOUT_ICMP_MAX,
382                 .obj_size       = sizeof(unsigned int),
383                 .nla_policy     = icmp_timeout_nla_policy,
384         },
385 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
386         .init_net               = icmp_init_net,
387         .get_net_proto          = icmp_get_net_proto,
388 };