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