]>
Commit | Line | Data |
---|---|---|
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 | 26 | static const unsigned int nf_ct_icmp_timeout = 30*HZ; |
9fb9cbb1 | 27 | |
09f263cd | 28 | static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
a31f1adc | 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. */ |
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 | ||
09f263cd JE |
57 | static bool icmp_invert_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 YK |
70 | /* Returns verdict for packet, or -1 for invalid. */ |
71 | static int icmp_packet(struct nf_conn *ct, | |
83d213fd | 72 | struct sk_buff *skb, |
9fb9cbb1 | 73 | unsigned int dataoff, |
93e66024 FW |
74 | enum ip_conntrack_info ctinfo, |
75 | const struct nf_hook_state *state) | |
9fb9cbb1 | 76 | { |
f87fb666 JK |
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. */ | |
c779e849 | 80 | unsigned int *timeout = nf_ct_timeout_lookup(ct); |
c1d10adb PNA |
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 | }; | |
9fb9cbb1 | 87 | |
dd2934a9 FW |
88 | if (state->pf != NFPROTO_IPV4) |
89 | return -NF_ACCEPT; | |
90 | ||
3666ed1c JP |
91 | if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) || |
92 | !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) { | |
9fb9cbb1 | 93 | /* Can't create a new ICMP `conn' with this. */ |
0d53778e | 94 | pr_debug("icmp: can't create new conn with type %u\n", |
c88130bc | 95 | ct->tuplehash[0].tuple.dst.u.icmp.type); |
3c9fba65 | 96 | nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple); |
9976fc6e | 97 | return -NF_ACCEPT; |
9fb9cbb1 | 98 | } |
9976fc6e FW |
99 | |
100 | if (!timeout) | |
a95a7774 | 101 | timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout; |
9976fc6e FW |
102 | |
103 | nf_ct_refresh_acct(ct, ctinfo, skb, *timeout); | |
104 | return NF_ACCEPT; | |
9fb9cbb1 YK |
105 | } |
106 | ||
9fb9cbb1 YK |
107 | /* Returns conntrack if it dealt with ICMP, and filled in skb fields */ |
108 | static int | |
93e66024 FW |
109 | icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb, |
110 | const struct nf_hook_state *state) | |
9fb9cbb1 YK |
111 | { |
112 | struct nf_conntrack_tuple innertuple, origtuple; | |
7cc3864d JE |
113 | const struct nf_conntrack_l4proto *innerproto; |
114 | const struct nf_conntrack_tuple_hash *h; | |
308ac914 | 115 | const struct nf_conntrack_zone *zone; |
11df4b76 | 116 | enum ip_conntrack_info ctinfo; |
5e8018fc | 117 | struct nf_conntrack_zone tmp; |
9fb9cbb1 | 118 | |
44d6e2f2 | 119 | WARN_ON(skb_nfct(skb)); |
5e8018fc | 120 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
9fb9cbb1 | 121 | |
e2a3123f YK |
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), | |
93e66024 | 126 | PF_INET, state->net, &origtuple)) { |
e2a3123f | 127 | pr_debug("icmp_error_message: failed to get tuple\n"); |
9fb9cbb1 YK |
128 | return -NF_ACCEPT; |
129 | } | |
130 | ||
e2361cb9 | 131 | /* rcu_read_lock()ed by nf_hook_thresh */ |
dd2934a9 | 132 | innerproto = __nf_ct_l4proto_find(origtuple.dst.protonum); |
9fb9cbb1 | 133 | |
e905a9ed YH |
134 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
135 | been preserved inside the ICMP. */ | |
d1b6fe94 | 136 | if (!nf_ct_invert_tuple(&innertuple, &origtuple, innerproto)) { |
0d53778e | 137 | pr_debug("icmp_error_message: no match\n"); |
9fb9cbb1 YK |
138 | return -NF_ACCEPT; |
139 | } | |
140 | ||
11df4b76 | 141 | ctinfo = IP_CT_RELATED; |
9fb9cbb1 | 142 | |
93e66024 | 143 | h = nf_conntrack_find_get(state->net, zone, &innertuple); |
9fb9cbb1 | 144 | if (!h) { |
130e7a83 YK |
145 | pr_debug("icmp_error_message: no match\n"); |
146 | return -NF_ACCEPT; | |
9fb9cbb1 YK |
147 | } |
148 | ||
130e7a83 | 149 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
11df4b76 | 150 | ctinfo += IP_CT_IS_REPLY; |
130e7a83 | 151 | |
e905a9ed | 152 | /* Update skb to refer to this connection */ |
c74454fa | 153 | nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo); |
88ed01d1 | 154 | return NF_ACCEPT; |
9fb9cbb1 YK |
155 | } |
156 | ||
93e66024 FW |
157 | static void icmp_error_log(const struct sk_buff *skb, |
158 | const struct nf_hook_state *state, | |
159 | const char *msg) | |
c4f3db15 | 160 | { |
93e66024 FW |
161 | nf_l4proto_log_invalid(skb, state->net, state->pf, |
162 | IPPROTO_ICMP, "%s", msg); | |
c4f3db15 FW |
163 | } |
164 | ||
9fb9cbb1 | 165 | /* Small and modified version of icmp_rcv */ |
6fe78fa4 FW |
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) | |
9fb9cbb1 | 169 | { |
7cc3864d JE |
170 | const struct icmphdr *icmph; |
171 | struct icmphdr _ih; | |
9fb9cbb1 YK |
172 | |
173 | /* Not enough header? */ | |
c9bdd4b5 | 174 | icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih); |
9fb9cbb1 | 175 | if (icmph == NULL) { |
93e66024 | 176 | icmp_error_log(skb, state, "short packet"); |
9fb9cbb1 YK |
177 | return -NF_ACCEPT; |
178 | } | |
179 | ||
180 | /* See ip_conntrack_proto_tcp.c */ | |
93e66024 FW |
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"); | |
9fb9cbb1 | 185 | return -NF_ACCEPT; |
9fb9cbb1 YK |
186 | } |
187 | ||
9fb9cbb1 YK |
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) { | |
93e66024 | 195 | icmp_error_log(skb, state, "invalid icmp type"); |
9fb9cbb1 YK |
196 | return -NF_ACCEPT; |
197 | } | |
198 | ||
199 | /* Need to track icmp error message? */ | |
3666ed1c JP |
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) | |
9fb9cbb1 YK |
205 | return NF_ACCEPT; |
206 | ||
93e66024 | 207 | return icmp_error_message(tmpl, skb, state); |
9fb9cbb1 YK |
208 | } |
209 | ||
24de3d37 | 210 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c1d10adb PNA |
211 | |
212 | #include <linux/netfilter/nfnetlink.h> | |
213 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
214 | ||
fdf70832 | 215 | static int icmp_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
216 | const struct nf_conntrack_tuple *t) |
217 | { | |
d317e4f6 DM |
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; | |
c1d10adb PNA |
222 | return 0; |
223 | ||
df6fb868 | 224 | nla_put_failure: |
c1d10adb PNA |
225 | return -1; |
226 | } | |
227 | ||
f73e924c PM |
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 }, | |
c1d10adb PNA |
232 | }; |
233 | ||
fdf70832 | 234 | static int icmp_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
235 | struct nf_conntrack_tuple *tuple) |
236 | { | |
3666ed1c JP |
237 | if (!tb[CTA_PROTO_ICMP_TYPE] || |
238 | !tb[CTA_PROTO_ICMP_CODE] || | |
239 | !tb[CTA_PROTO_ICMP_ID]) | |
c1d10adb PNA |
240 | return -EINVAL; |
241 | ||
77236b6e PM |
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]); | |
c1d10adb | 245 | |
3666ed1c JP |
246 | if (tuple->dst.u.icmp.type >= sizeof(invmap) || |
247 | !invmap[tuple->dst.u.icmp.type]) | |
c1d10adb PNA |
248 | return -EINVAL; |
249 | ||
250 | return 0; | |
251 | } | |
a400c30e | 252 | |
5caaed15 | 253 | static unsigned int icmp_nlattr_tuple_size(void) |
a400c30e | 254 | { |
5caaed15 FW |
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; | |
a400c30e | 261 | } |
c1d10adb PNA |
262 | #endif |
263 | ||
a874752a | 264 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
265 | |
266 | #include <linux/netfilter/nfnetlink.h> | |
267 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
268 | ||
8264deb8 G |
269 | static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[], |
270 | struct net *net, void *data) | |
50978462 PNA |
271 | { |
272 | unsigned int *timeout = data; | |
a95a7774 | 273 | struct nf_icmp_net *in = nf_icmp_pernet(net); |
50978462 PNA |
274 | |
275 | if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) { | |
c779e849 FW |
276 | if (!timeout) |
277 | timeout = &in->timeout; | |
50978462 PNA |
278 | *timeout = |
279 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ; | |
c779e849 | 280 | } else if (timeout) { |
50978462 | 281 | /* Set default ICMP timeout. */ |
8264deb8 | 282 | *timeout = in->timeout; |
50978462 PNA |
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 | ||
d317e4f6 DM |
292 | if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ))) |
293 | goto nla_put_failure; | |
50978462 PNA |
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 | }; | |
a874752a | 304 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
50978462 | 305 | |
933a41e7 | 306 | #ifdef CONFIG_SYSCTL |
933a41e7 PM |
307 | static struct ctl_table icmp_sysctl_table[] = { |
308 | { | |
933a41e7 | 309 | .procname = "nf_conntrack_icmp_timeout", |
933a41e7 PM |
310 | .maxlen = sizeof(unsigned int), |
311 | .mode = 0644, | |
6d9f239a | 312 | .proc_handler = proc_dointvec_jiffies, |
933a41e7 | 313 | }, |
f8572d8f | 314 | { } |
933a41e7 PM |
315 | }; |
316 | #endif /* CONFIG_SYSCTL */ | |
317 | ||
a9082b45 G |
318 | static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn, |
319 | struct nf_icmp_net *in) | |
4b626b9c | 320 | { |
4b626b9c G |
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; | |
a9082b45 | 327 | |
4b626b9c | 328 | pn->ctl_table[0].data = &in->timeout; |
a9082b45 G |
329 | #endif |
330 | return 0; | |
331 | } | |
332 | ||
ca2ca6e1 | 333 | static int icmp_init_net(struct net *net) |
a9082b45 | 334 | { |
a95a7774 | 335 | struct nf_icmp_net *in = nf_icmp_pernet(net); |
a9082b45 G |
336 | struct nf_proto_net *pn = &in->pn; |
337 | ||
338 | in->timeout = nf_ct_icmp_timeout; | |
339 | ||
adf05168 | 340 | return icmp_kmemdup_sysctl_table(pn, in); |
a9082b45 G |
341 | } |
342 | ||
08911475 PNA |
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 | ||
9dae47ab | 348 | const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp = |
9fb9cbb1 | 349 | { |
605dcad6 | 350 | .l4proto = IPPROTO_ICMP, |
9fb9cbb1 YK |
351 | .pkt_to_tuple = icmp_pkt_to_tuple, |
352 | .invert_tuple = icmp_invert_tuple, | |
9fb9cbb1 | 353 | .packet = icmp_packet, |
9fb9cbb1 | 354 | .destroy = NULL, |
c1d10adb | 355 | .me = NULL, |
24de3d37 | 356 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 357 | .tuple_to_nlattr = icmp_tuple_to_nlattr, |
a400c30e | 358 | .nlattr_tuple_size = icmp_nlattr_tuple_size, |
fdf70832 | 359 | .nlattr_to_tuple = icmp_nlattr_to_tuple, |
f73e924c | 360 | .nla_policy = icmp_nla_policy, |
c1d10adb | 361 | #endif |
a874752a | 362 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
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 | }, | |
a874752a | 370 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
4b626b9c | 371 | .init_net = icmp_init_net, |
08911475 | 372 | .get_net_proto = icmp_get_net_proto, |
9fb9cbb1 | 373 | }; |