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 inline struct nf_icmp_net *icmp_pernet(struct net *net)
30 return &net->ct.nf_ct_proto.icmp;
33 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
34 struct net *net, struct nf_conntrack_tuple *tuple)
36 const struct icmphdr *hp;
39 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
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;
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
62 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
63 const struct nf_conntrack_tuple *orig)
65 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
66 !invmap[orig->dst.u.icmp.type])
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;
75 /* Returns verdict for packet, or -1 for invalid. */
76 static int icmp_packet(struct nf_conn *ct,
79 enum ip_conntrack_info ctinfo,
80 const struct nf_hook_state *state)
82 /* Do not immediately delete the connection after the first
83 successful reply to avoid excessive conntrackd traffic
84 and also to handle correctly ICMP echo reply duplicates. */
85 unsigned int *timeout = nf_ct_timeout_lookup(ct);
86 static const u_int8_t valid_new[] = {
89 [ICMP_INFO_REQUEST] = 1,
93 if (state->pf != NFPROTO_IPV4)
96 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
97 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
98 /* Can't create a new ICMP `conn' with this. */
99 pr_debug("icmp: can't create new conn with type %u\n",
100 ct->tuplehash[0].tuple.dst.u.icmp.type);
101 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
106 timeout = &icmp_pernet(nf_ct_net(ct))->timeout;
108 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
112 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
114 icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
115 const struct nf_hook_state *state)
117 struct nf_conntrack_tuple innertuple, origtuple;
118 const struct nf_conntrack_l4proto *innerproto;
119 const struct nf_conntrack_tuple_hash *h;
120 const struct nf_conntrack_zone *zone;
121 enum ip_conntrack_info ctinfo;
122 struct nf_conntrack_zone tmp;
124 WARN_ON(skb_nfct(skb));
125 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
127 /* Are they talking about one of our connections? */
128 if (!nf_ct_get_tuplepr(skb,
129 skb_network_offset(skb) + ip_hdrlen(skb)
130 + sizeof(struct icmphdr),
131 PF_INET, state->net, &origtuple)) {
132 pr_debug("icmp_error_message: failed to get tuple\n");
136 /* rcu_read_lock()ed by nf_hook_thresh */
137 innerproto = __nf_ct_l4proto_find(origtuple.dst.protonum);
139 /* Ordinarily, we'd expect the inverted tupleproto, but it's
140 been preserved inside the ICMP. */
141 if (!nf_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
142 pr_debug("icmp_error_message: no match\n");
146 ctinfo = IP_CT_RELATED;
148 h = nf_conntrack_find_get(state->net, zone, &innertuple);
150 pr_debug("icmp_error_message: no match\n");
154 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
155 ctinfo += IP_CT_IS_REPLY;
157 /* Update skb to refer to this connection */
158 nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
162 static void icmp_error_log(const struct sk_buff *skb,
163 const struct nf_hook_state *state,
166 nf_l4proto_log_invalid(skb, state->net, state->pf,
167 IPPROTO_ICMP, "%s", msg);
170 /* Small and modified version of icmp_rcv */
171 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
172 struct sk_buff *skb, unsigned int dataoff,
173 const struct nf_hook_state *state)
175 const struct icmphdr *icmph;
178 /* Not enough header? */
179 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
181 icmp_error_log(skb, state, "short packet");
185 /* See ip_conntrack_proto_tcp.c */
186 if (state->net->ct.sysctl_checksum &&
187 state->hook == NF_INET_PRE_ROUTING &&
188 nf_ip_checksum(skb, state->hook, dataoff, 0)) {
189 icmp_error_log(skb, state, "bad hw icmp checksum");
194 * 18 is the highest 'known' ICMP type. Anything else is a mystery
196 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
199 if (icmph->type > NR_ICMP_TYPES) {
200 icmp_error_log(skb, state, "invalid icmp type");
204 /* Need to track icmp error message? */
205 if (icmph->type != ICMP_DEST_UNREACH &&
206 icmph->type != ICMP_SOURCE_QUENCH &&
207 icmph->type != ICMP_TIME_EXCEEDED &&
208 icmph->type != ICMP_PARAMETERPROB &&
209 icmph->type != ICMP_REDIRECT)
212 return icmp_error_message(tmpl, skb, state);
215 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
217 #include <linux/netfilter/nfnetlink.h>
218 #include <linux/netfilter/nfnetlink_conntrack.h>
220 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
221 const struct nf_conntrack_tuple *t)
223 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
224 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
225 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
226 goto nla_put_failure;
233 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
234 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
235 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
236 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
239 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
240 struct nf_conntrack_tuple *tuple)
242 if (!tb[CTA_PROTO_ICMP_TYPE] ||
243 !tb[CTA_PROTO_ICMP_CODE] ||
244 !tb[CTA_PROTO_ICMP_ID])
247 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
248 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
249 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
251 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
252 !invmap[tuple->dst.u.icmp.type])
258 static unsigned int icmp_nlattr_tuple_size(void)
260 static unsigned int size __read_mostly;
263 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
269 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
271 #include <linux/netfilter/nfnetlink.h>
272 #include <linux/netfilter/nfnetlink_cttimeout.h>
274 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
275 struct net *net, void *data)
277 unsigned int *timeout = data;
278 struct nf_icmp_net *in = icmp_pernet(net);
280 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
282 timeout = &in->timeout;
284 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
285 } else if (timeout) {
286 /* Set default ICMP timeout. */
287 *timeout = in->timeout;
293 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
295 const unsigned int *timeout = data;
297 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
298 goto nla_put_failure;
305 static const struct nla_policy
306 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
307 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
309 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
312 static struct ctl_table icmp_sysctl_table[] = {
314 .procname = "nf_conntrack_icmp_timeout",
315 .maxlen = sizeof(unsigned int),
317 .proc_handler = proc_dointvec_jiffies,
321 #endif /* CONFIG_SYSCTL */
323 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
324 struct nf_icmp_net *in)
327 pn->ctl_table = kmemdup(icmp_sysctl_table,
328 sizeof(icmp_sysctl_table),
333 pn->ctl_table[0].data = &in->timeout;
338 static int icmp_init_net(struct net *net)
340 struct nf_icmp_net *in = icmp_pernet(net);
341 struct nf_proto_net *pn = &in->pn;
343 in->timeout = nf_ct_icmp_timeout;
345 return icmp_kmemdup_sysctl_table(pn, in);
348 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
350 return &net->ct.nf_ct_proto.icmp.pn;
353 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
355 .l4proto = IPPROTO_ICMP,
356 .pkt_to_tuple = icmp_pkt_to_tuple,
357 .invert_tuple = icmp_invert_tuple,
358 .packet = icmp_packet,
361 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
362 .tuple_to_nlattr = icmp_tuple_to_nlattr,
363 .nlattr_tuple_size = icmp_nlattr_tuple_size,
364 .nlattr_to_tuple = icmp_nlattr_to_tuple,
365 .nla_policy = icmp_nla_policy,
367 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
369 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
370 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
371 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
372 .obj_size = sizeof(unsigned int),
373 .nla_policy = icmp_timeout_nla_policy,
375 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
376 .init_net = icmp_init_net,
377 .get_net_proto = icmp_get_net_proto,