]> git.ipfire.org Git - thirdparty/linux.git/blame - net/netfilter/nf_conntrack_proto_icmp.c
mm/hotplug: treat CMA pages as unmovable
[thirdparty/linux.git] / net / 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>
f229f6ce 3 * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
9fb9cbb1
YK
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.
9fb9cbb1
YK
8 */
9
10#include <linux/types.h>
9fb9cbb1
YK
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>
605dcad6 20#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1 21#include <net/netfilter/nf_conntrack_core.h>
c779e849 22#include <net/netfilter/nf_conntrack_timeout.h>
5d0aa2cc 23#include <net/netfilter/nf_conntrack_zones.h>
f01ffbd6 24#include <net/netfilter/nf_log.h>
9fb9cbb1 25
2c9e8637 26static const unsigned int nf_ct_icmp_timeout = 30*HZ;
9fb9cbb1 27
e2e48b47
FW
28bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
29 struct net *net, struct nf_conntrack_tuple *tuple)
9fb9cbb1 30{
7cc3864d
JE
31 const struct icmphdr *hp;
32 struct icmphdr _hdr;
9fb9cbb1
YK
33
34 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
35 if (hp == NULL)
09f263cd 36 return false;
9fb9cbb1
YK
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
09f263cd 42 return true;
9fb9cbb1
YK
43}
44
c1d10adb
PNA
45/* Add 1; spaces filled with 0. */
46static 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
197c4300
FW
57bool nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple *tuple,
58 const struct nf_conntrack_tuple *orig)
9fb9cbb1 59{
3666ed1c
JP
60 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
61 !invmap[orig->dst.u.icmp.type])
09f263cd 62 return false;
9fb9cbb1
YK
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;
09f263cd 67 return true;
9fb9cbb1
YK
68}
69
9fb9cbb1 70/* Returns verdict for packet, or -1 for invalid. */
a47c5404
FW
71int nf_conntrack_icmp_packet(struct nf_conn *ct,
72 struct sk_buff *skb,
73 enum ip_conntrack_info ctinfo,
74 const struct nf_hook_state *state)
9fb9cbb1 75{
f87fb666
JK
76 /* Do not immediately delete the connection after the first
77 successful reply to avoid excessive conntrackd traffic
78 and also to handle correctly ICMP echo reply duplicates. */
c779e849 79 unsigned int *timeout = nf_ct_timeout_lookup(ct);
c1d10adb
PNA
80 static const u_int8_t valid_new[] = {
81 [ICMP_ECHO] = 1,
82 [ICMP_TIMESTAMP] = 1,
83 [ICMP_INFO_REQUEST] = 1,
84 [ICMP_ADDRESS] = 1
85 };
9fb9cbb1 86
dd2934a9
FW
87 if (state->pf != NFPROTO_IPV4)
88 return -NF_ACCEPT;
89
3666ed1c
JP
90 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
91 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
9fb9cbb1 92 /* Can't create a new ICMP `conn' with this. */
0d53778e 93 pr_debug("icmp: can't create new conn with type %u\n",
c88130bc 94 ct->tuplehash[0].tuple.dst.u.icmp.type);
3c9fba65 95 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
9976fc6e 96 return -NF_ACCEPT;
9fb9cbb1 97 }
9976fc6e
FW
98
99 if (!timeout)
a95a7774 100 timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout;
9976fc6e
FW
101
102 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
103 return NF_ACCEPT;
9fb9cbb1
YK
104}
105
9fb9cbb1
YK
106/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
107static int
93e66024
FW
108icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
109 const struct nf_hook_state *state)
9fb9cbb1
YK
110{
111 struct nf_conntrack_tuple innertuple, origtuple;
7cc3864d 112 const struct nf_conntrack_tuple_hash *h;
308ac914 113 const struct nf_conntrack_zone *zone;
11df4b76 114 enum ip_conntrack_info ctinfo;
5e8018fc 115 struct nf_conntrack_zone tmp;
9fb9cbb1 116
44d6e2f2 117 WARN_ON(skb_nfct(skb));
5e8018fc 118 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
9fb9cbb1 119
e2a3123f
YK
120 /* Are they talking about one of our connections? */
121 if (!nf_ct_get_tuplepr(skb,
122 skb_network_offset(skb) + ip_hdrlen(skb)
123 + sizeof(struct icmphdr),
93e66024 124 PF_INET, state->net, &origtuple)) {
e2a3123f 125 pr_debug("icmp_error_message: failed to get tuple\n");
9fb9cbb1
YK
126 return -NF_ACCEPT;
127 }
128
e905a9ed
YH
129 /* Ordinarily, we'd expect the inverted tupleproto, but it's
130 been preserved inside the ICMP. */
303e0c55 131 if (!nf_ct_invert_tuple(&innertuple, &origtuple)) {
0d53778e 132 pr_debug("icmp_error_message: no match\n");
9fb9cbb1
YK
133 return -NF_ACCEPT;
134 }
135
11df4b76 136 ctinfo = IP_CT_RELATED;
9fb9cbb1 137
93e66024 138 h = nf_conntrack_find_get(state->net, zone, &innertuple);
9fb9cbb1 139 if (!h) {
130e7a83
YK
140 pr_debug("icmp_error_message: no match\n");
141 return -NF_ACCEPT;
9fb9cbb1
YK
142 }
143
130e7a83 144 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
11df4b76 145 ctinfo += IP_CT_IS_REPLY;
130e7a83 146
e905a9ed 147 /* Update skb to refer to this connection */
c74454fa 148 nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
88ed01d1 149 return NF_ACCEPT;
9fb9cbb1
YK
150}
151
93e66024
FW
152static void icmp_error_log(const struct sk_buff *skb,
153 const struct nf_hook_state *state,
154 const char *msg)
c4f3db15 155{
93e66024
FW
156 nf_l4proto_log_invalid(skb, state->net, state->pf,
157 IPPROTO_ICMP, "%s", msg);
c4f3db15
FW
158}
159
9fb9cbb1 160/* Small and modified version of icmp_rcv */
6fe78fa4
FW
161int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
162 struct sk_buff *skb, unsigned int dataoff,
163 const struct nf_hook_state *state)
9fb9cbb1 164{
7cc3864d
JE
165 const struct icmphdr *icmph;
166 struct icmphdr _ih;
9fb9cbb1
YK
167
168 /* Not enough header? */
c9bdd4b5 169 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
9fb9cbb1 170 if (icmph == NULL) {
93e66024 171 icmp_error_log(skb, state, "short packet");
9fb9cbb1
YK
172 return -NF_ACCEPT;
173 }
174
175 /* See ip_conntrack_proto_tcp.c */
93e66024
FW
176 if (state->net->ct.sysctl_checksum &&
177 state->hook == NF_INET_PRE_ROUTING &&
178 nf_ip_checksum(skb, state->hook, dataoff, 0)) {
179 icmp_error_log(skb, state, "bad hw icmp checksum");
9fb9cbb1 180 return -NF_ACCEPT;
9fb9cbb1
YK
181 }
182
9fb9cbb1
YK
183 /*
184 * 18 is the highest 'known' ICMP type. Anything else is a mystery
185 *
186 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
187 * discarded.
188 */
189 if (icmph->type > NR_ICMP_TYPES) {
93e66024 190 icmp_error_log(skb, state, "invalid icmp type");
9fb9cbb1
YK
191 return -NF_ACCEPT;
192 }
193
194 /* Need to track icmp error message? */
3666ed1c
JP
195 if (icmph->type != ICMP_DEST_UNREACH &&
196 icmph->type != ICMP_SOURCE_QUENCH &&
197 icmph->type != ICMP_TIME_EXCEEDED &&
198 icmph->type != ICMP_PARAMETERPROB &&
199 icmph->type != ICMP_REDIRECT)
9fb9cbb1
YK
200 return NF_ACCEPT;
201
93e66024 202 return icmp_error_message(tmpl, skb, state);
9fb9cbb1
YK
203}
204
24de3d37 205#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
c1d10adb
PNA
206
207#include <linux/netfilter/nfnetlink.h>
208#include <linux/netfilter/nfnetlink_conntrack.h>
209
fdf70832 210static int icmp_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
211 const struct nf_conntrack_tuple *t)
212{
d317e4f6
DM
213 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
214 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
215 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
216 goto nla_put_failure;
c1d10adb
PNA
217 return 0;
218
df6fb868 219nla_put_failure:
c1d10adb
PNA
220 return -1;
221}
222
f73e924c
PM
223static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
224 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
225 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
226 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
c1d10adb
PNA
227};
228
fdf70832 229static int icmp_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
230 struct nf_conntrack_tuple *tuple)
231{
3666ed1c
JP
232 if (!tb[CTA_PROTO_ICMP_TYPE] ||
233 !tb[CTA_PROTO_ICMP_CODE] ||
234 !tb[CTA_PROTO_ICMP_ID])
c1d10adb
PNA
235 return -EINVAL;
236
77236b6e
PM
237 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
238 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
239 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
c1d10adb 240
3666ed1c
JP
241 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
242 !invmap[tuple->dst.u.icmp.type])
c1d10adb
PNA
243 return -EINVAL;
244
245 return 0;
246}
a400c30e 247
5caaed15 248static unsigned int icmp_nlattr_tuple_size(void)
a400c30e 249{
5caaed15
FW
250 static unsigned int size __read_mostly;
251
252 if (!size)
253 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
254
255 return size;
a400c30e 256}
c1d10adb
PNA
257#endif
258
a874752a 259#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
260
261#include <linux/netfilter/nfnetlink.h>
262#include <linux/netfilter/nfnetlink_cttimeout.h>
263
8264deb8
G
264static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
265 struct net *net, void *data)
50978462
PNA
266{
267 unsigned int *timeout = data;
a95a7774 268 struct nf_icmp_net *in = nf_icmp_pernet(net);
50978462
PNA
269
270 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
c779e849
FW
271 if (!timeout)
272 timeout = &in->timeout;
50978462
PNA
273 *timeout =
274 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
c779e849 275 } else if (timeout) {
50978462 276 /* Set default ICMP timeout. */
8264deb8 277 *timeout = in->timeout;
50978462
PNA
278 }
279 return 0;
280}
281
282static int
283icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
284{
285 const unsigned int *timeout = data;
286
d317e4f6
DM
287 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
288 goto nla_put_failure;
50978462
PNA
289 return 0;
290
291nla_put_failure:
292 return -ENOSPC;
293}
294
295static const struct nla_policy
296icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
297 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
298};
a874752a 299#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
50978462 300
2a389de8 301void nf_conntrack_icmp_init_net(struct net *net)
a9082b45 302{
a95a7774 303 struct nf_icmp_net *in = nf_icmp_pernet(net);
a9082b45
G
304
305 in->timeout = nf_ct_icmp_timeout;
08911475
PNA
306}
307
9dae47ab 308const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
9fb9cbb1 309{
605dcad6 310 .l4proto = IPPROTO_ICMP,
24de3d37 311#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
fdf70832 312 .tuple_to_nlattr = icmp_tuple_to_nlattr,
a400c30e 313 .nlattr_tuple_size = icmp_nlattr_tuple_size,
fdf70832 314 .nlattr_to_tuple = icmp_nlattr_to_tuple,
f73e924c 315 .nla_policy = icmp_nla_policy,
c1d10adb 316#endif
a874752a 317#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
50978462
PNA
318 .ctnl_timeout = {
319 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
320 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
321 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
322 .obj_size = sizeof(unsigned int),
323 .nla_policy = icmp_timeout_nla_policy,
324 },
a874752a 325#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
9fb9cbb1 326};