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 static unsigned int *icmp_get_timeouts(struct net *net)
77 return &icmp_pernet(net)->timeout;
80 /* Returns verdict for packet, or -1 for invalid. */
81 static int icmp_packet(struct nf_conn *ct,
82 const struct sk_buff *skb,
84 enum ip_conntrack_info ctinfo)
86 /* Do not immediately delete the connection after the first
87 successful reply to avoid excessive conntrackd traffic
88 and also to handle correctly ICMP echo reply duplicates. */
89 unsigned int *timeout = nf_ct_timeout_lookup(ct);
92 timeout = icmp_get_timeouts(nf_ct_net(ct));
94 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
99 /* Called when a new connection for this protocol found. */
100 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
101 unsigned int dataoff)
103 static const u_int8_t valid_new[] = {
105 [ICMP_TIMESTAMP] = 1,
106 [ICMP_INFO_REQUEST] = 1,
110 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
111 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
112 /* Can't create a new ICMP `conn' with this. */
113 pr_debug("icmp: can't create new conn with type %u\n",
114 ct->tuplehash[0].tuple.dst.u.icmp.type);
115 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
121 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
123 icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
124 unsigned int hooknum)
126 struct nf_conntrack_tuple innertuple, origtuple;
127 const struct nf_conntrack_l4proto *innerproto;
128 const struct nf_conntrack_tuple_hash *h;
129 const struct nf_conntrack_zone *zone;
130 enum ip_conntrack_info ctinfo;
131 struct nf_conntrack_zone tmp;
133 WARN_ON(skb_nfct(skb));
134 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
136 /* Are they talking about one of our connections? */
137 if (!nf_ct_get_tuplepr(skb,
138 skb_network_offset(skb) + ip_hdrlen(skb)
139 + sizeof(struct icmphdr),
140 PF_INET, net, &origtuple)) {
141 pr_debug("icmp_error_message: failed to get tuple\n");
145 /* rcu_read_lock()ed by nf_hook_thresh */
146 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
148 /* Ordinarily, we'd expect the inverted tupleproto, but it's
149 been preserved inside the ICMP. */
150 if (!nf_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
151 pr_debug("icmp_error_message: no match\n");
155 ctinfo = IP_CT_RELATED;
157 h = nf_conntrack_find_get(net, zone, &innertuple);
159 pr_debug("icmp_error_message: no match\n");
163 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
164 ctinfo += IP_CT_IS_REPLY;
166 /* Update skb to refer to this connection */
167 nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
171 static void icmp_error_log(const struct sk_buff *skb, struct net *net,
172 u8 pf, const char *msg)
174 nf_l4proto_log_invalid(skb, net, pf, IPPROTO_ICMP, "%s", msg);
177 /* Small and modified version of icmp_rcv */
179 icmp_error(struct net *net, struct nf_conn *tmpl,
180 struct sk_buff *skb, unsigned int dataoff,
181 u8 pf, unsigned int hooknum)
183 const struct icmphdr *icmph;
186 /* Not enough header? */
187 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
189 icmp_error_log(skb, net, pf, "short packet");
193 /* See ip_conntrack_proto_tcp.c */
194 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
195 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
196 icmp_error_log(skb, net, pf, "bad hw icmp checksum");
201 * 18 is the highest 'known' ICMP type. Anything else is a mystery
203 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
206 if (icmph->type > NR_ICMP_TYPES) {
207 icmp_error_log(skb, net, pf, "invalid icmp type");
211 /* Need to track icmp error message? */
212 if (icmph->type != ICMP_DEST_UNREACH &&
213 icmph->type != ICMP_SOURCE_QUENCH &&
214 icmph->type != ICMP_TIME_EXCEEDED &&
215 icmph->type != ICMP_PARAMETERPROB &&
216 icmph->type != ICMP_REDIRECT)
219 return icmp_error_message(net, tmpl, skb, hooknum);
222 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
224 #include <linux/netfilter/nfnetlink.h>
225 #include <linux/netfilter/nfnetlink_conntrack.h>
227 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
228 const struct nf_conntrack_tuple *t)
230 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
231 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
232 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
233 goto nla_put_failure;
240 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
241 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
242 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
243 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
246 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
247 struct nf_conntrack_tuple *tuple)
249 if (!tb[CTA_PROTO_ICMP_TYPE] ||
250 !tb[CTA_PROTO_ICMP_CODE] ||
251 !tb[CTA_PROTO_ICMP_ID])
254 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
255 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
256 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
258 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
259 !invmap[tuple->dst.u.icmp.type])
265 static unsigned int icmp_nlattr_tuple_size(void)
267 static unsigned int size __read_mostly;
270 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
276 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
278 #include <linux/netfilter/nfnetlink.h>
279 #include <linux/netfilter/nfnetlink_cttimeout.h>
281 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
282 struct net *net, void *data)
284 unsigned int *timeout = data;
285 struct nf_icmp_net *in = icmp_pernet(net);
287 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
289 timeout = &in->timeout;
291 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
292 } else if (timeout) {
293 /* Set default ICMP timeout. */
294 *timeout = in->timeout;
300 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
302 const unsigned int *timeout = data;
304 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
305 goto nla_put_failure;
312 static const struct nla_policy
313 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
314 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
316 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
319 static struct ctl_table icmp_sysctl_table[] = {
321 .procname = "nf_conntrack_icmp_timeout",
322 .maxlen = sizeof(unsigned int),
324 .proc_handler = proc_dointvec_jiffies,
328 #endif /* CONFIG_SYSCTL */
330 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
331 struct nf_icmp_net *in)
334 pn->ctl_table = kmemdup(icmp_sysctl_table,
335 sizeof(icmp_sysctl_table),
340 pn->ctl_table[0].data = &in->timeout;
345 static int icmp_init_net(struct net *net, u_int16_t proto)
347 struct nf_icmp_net *in = icmp_pernet(net);
348 struct nf_proto_net *pn = &in->pn;
350 in->timeout = nf_ct_icmp_timeout;
352 return icmp_kmemdup_sysctl_table(pn, in);
355 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
357 return &net->ct.nf_ct_proto.icmp.pn;
360 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
363 .l4proto = IPPROTO_ICMP,
364 .pkt_to_tuple = icmp_pkt_to_tuple,
365 .invert_tuple = icmp_invert_tuple,
366 .packet = icmp_packet,
371 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
372 .tuple_to_nlattr = icmp_tuple_to_nlattr,
373 .nlattr_tuple_size = icmp_nlattr_tuple_size,
374 .nlattr_to_tuple = icmp_nlattr_to_tuple,
375 .nla_policy = icmp_nla_policy,
377 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
379 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
380 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
381 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
382 .obj_size = sizeof(unsigned int),
383 .nla_policy = icmp_timeout_nla_policy,
385 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
386 .init_net = icmp_init_net,
387 .get_net_proto = icmp_get_net_proto,