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>
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.
10 #include <linux/types.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
14 #include <linux/icmp.h>
15 #include <linux/seq_file.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>
26 static const unsigned int nf_ct_icmp_timeout = 30*HZ;
28 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
29 struct net *net, struct nf_conntrack_tuple *tuple)
31 const struct icmphdr *hp;
34 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
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;
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
57 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
58 const struct nf_conntrack_tuple *orig)
60 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
61 !invmap[orig->dst.u.icmp.type])
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;
70 /* Returns verdict for packet, or -1 for invalid. */
71 static int icmp_packet(struct nf_conn *ct,
74 enum ip_conntrack_info ctinfo,
75 const struct nf_hook_state *state)
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[] = {
84 [ICMP_INFO_REQUEST] = 1,
88 if (state->pf != NFPROTO_IPV4)
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);
101 timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout;
103 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
107 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
109 icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
110 const struct nf_hook_state *state)
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;
119 WARN_ON(skb_nfct(skb));
120 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
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");
131 /* rcu_read_lock()ed by nf_hook_thresh */
132 innerproto = __nf_ct_l4proto_find(origtuple.dst.protonum);
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");
141 ctinfo = IP_CT_RELATED;
143 h = nf_conntrack_find_get(state->net, zone, &innertuple);
145 pr_debug("icmp_error_message: no match\n");
149 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
150 ctinfo += IP_CT_IS_REPLY;
152 /* Update skb to refer to this connection */
153 nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
157 static void icmp_error_log(const struct sk_buff *skb,
158 const struct nf_hook_state *state,
161 nf_l4proto_log_invalid(skb, state->net, state->pf,
162 IPPROTO_ICMP, "%s", msg);
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)
170 const struct icmphdr *icmph;
173 /* Not enough header? */
174 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
176 icmp_error_log(skb, state, "short packet");
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");
189 * 18 is the highest 'known' ICMP type. Anything else is a mystery
191 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
194 if (icmph->type > NR_ICMP_TYPES) {
195 icmp_error_log(skb, state, "invalid icmp type");
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)
207 return icmp_error_message(tmpl, skb, state);
210 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
212 #include <linux/netfilter/nfnetlink.h>
213 #include <linux/netfilter/nfnetlink_conntrack.h>
215 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
216 const struct nf_conntrack_tuple *t)
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;
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 },
234 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
235 struct nf_conntrack_tuple *tuple)
237 if (!tb[CTA_PROTO_ICMP_TYPE] ||
238 !tb[CTA_PROTO_ICMP_CODE] ||
239 !tb[CTA_PROTO_ICMP_ID])
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]);
246 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
247 !invmap[tuple->dst.u.icmp.type])
253 static unsigned int icmp_nlattr_tuple_size(void)
255 static unsigned int size __read_mostly;
258 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
264 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
266 #include <linux/netfilter/nfnetlink.h>
267 #include <linux/netfilter/nfnetlink_cttimeout.h>
269 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
270 struct net *net, void *data)
272 unsigned int *timeout = data;
273 struct nf_icmp_net *in = nf_icmp_pernet(net);
275 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
277 timeout = &in->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;
288 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
290 const unsigned int *timeout = data;
292 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
293 goto nla_put_failure;
300 static const struct nla_policy
301 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
302 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
304 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
307 static struct ctl_table icmp_sysctl_table[] = {
309 .procname = "nf_conntrack_icmp_timeout",
310 .maxlen = sizeof(unsigned int),
312 .proc_handler = proc_dointvec_jiffies,
316 #endif /* CONFIG_SYSCTL */
318 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
319 struct nf_icmp_net *in)
322 pn->ctl_table = kmemdup(icmp_sysctl_table,
323 sizeof(icmp_sysctl_table),
328 pn->ctl_table[0].data = &in->timeout;
333 static int icmp_init_net(struct net *net)
335 struct nf_icmp_net *in = nf_icmp_pernet(net);
336 struct nf_proto_net *pn = &in->pn;
338 in->timeout = nf_ct_icmp_timeout;
340 return icmp_kmemdup_sysctl_table(pn, in);
343 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
345 return &net->ct.nf_ct_proto.icmp.pn;
348 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
350 .l4proto = IPPROTO_ICMP,
351 .pkt_to_tuple = icmp_pkt_to_tuple,
352 .invert_tuple = icmp_invert_tuple,
353 .packet = icmp_packet,
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,
362 #ifdef CONFIG_NF_CONNTRACK_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,
370 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
371 .init_net = icmp_init_net,
372 .get_net_proto = icmp_get_net_proto,