1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/netfilter.h>
13 #include <linux/icmp.h>
14 #include <linux/seq_file.h>
16 #include <net/checksum.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/netfilter/nf_conntrack_tuple.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_core.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
22 #include <net/netfilter/nf_log.h>
24 static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
26 static inline struct nf_icmp_net *icmp_pernet(struct net *net)
28 return &net->ct.nf_ct_proto.icmp;
31 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
32 struct nf_conntrack_tuple *tuple)
34 const struct icmphdr *hp;
37 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
41 tuple->dst.u.icmp.type = hp->type;
42 tuple->src.u.icmp.id = hp->un.echo.id;
43 tuple->dst.u.icmp.code = hp->code;
48 /* Add 1; spaces filled with 0. */
49 static const u_int8_t invmap[] = {
50 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
51 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
52 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
53 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
54 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
55 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
56 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
57 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
60 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
61 const struct nf_conntrack_tuple *orig)
63 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
64 !invmap[orig->dst.u.icmp.type])
67 tuple->src.u.icmp.id = orig->src.u.icmp.id;
68 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
69 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
73 /* Print out the per-protocol part of the tuple. */
74 static int icmp_print_tuple(struct seq_file *s,
75 const struct nf_conntrack_tuple *tuple)
77 return seq_printf(s, "type=%u code=%u id=%u ",
78 tuple->dst.u.icmp.type,
79 tuple->dst.u.icmp.code,
80 ntohs(tuple->src.u.icmp.id));
83 static unsigned int *icmp_get_timeouts(struct net *net)
85 return &icmp_pernet(net)->timeout;
88 /* Returns verdict for packet, or -1 for invalid. */
89 static int icmp_packet(struct nf_conn *ct,
90 const struct sk_buff *skb,
92 enum ip_conntrack_info ctinfo,
95 unsigned int *timeout)
97 /* Do not immediately delete the connection after the first
98 successful reply to avoid excessive conntrackd traffic
99 and also to handle correctly ICMP echo reply duplicates. */
100 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
105 /* Called when a new connection for this protocol found. */
106 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
107 unsigned int dataoff, unsigned int *timeouts)
109 static const u_int8_t valid_new[] = {
111 [ICMP_TIMESTAMP] = 1,
112 [ICMP_INFO_REQUEST] = 1,
116 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
117 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
118 /* Can't create a new ICMP `conn' with this. */
119 pr_debug("icmp: can't create new conn with type %u\n",
120 ct->tuplehash[0].tuple.dst.u.icmp.type);
121 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
127 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
129 icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
130 enum ip_conntrack_info *ctinfo,
131 unsigned int hooknum)
133 struct nf_conntrack_tuple innertuple, origtuple;
134 const struct nf_conntrack_l4proto *innerproto;
135 const struct nf_conntrack_tuple_hash *h;
136 u16 zone = tmpl ? nf_ct_zone(tmpl) : NF_CT_DEFAULT_ZONE;
138 NF_CT_ASSERT(skb->nfct == NULL);
140 /* Are they talking about one of our connections? */
141 if (!nf_ct_get_tuplepr(skb,
142 skb_network_offset(skb) + ip_hdrlen(skb)
143 + sizeof(struct icmphdr),
144 PF_INET, &origtuple)) {
145 pr_debug("icmp_error_message: failed to get tuple\n");
149 /* rcu_read_lock()ed by nf_hook_slow */
150 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
152 /* Ordinarily, we'd expect the inverted tupleproto, but it's
153 been preserved inside the ICMP. */
154 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
155 &nf_conntrack_l3proto_ipv4, innerproto)) {
156 pr_debug("icmp_error_message: no match\n");
160 *ctinfo = IP_CT_RELATED;
162 h = nf_conntrack_find_get(net, zone, &innertuple);
164 pr_debug("icmp_error_message: no match\n");
168 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
169 *ctinfo += IP_CT_IS_REPLY;
171 /* Update skb to refer to this connection */
172 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
173 skb->nfctinfo = *ctinfo;
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 enum ip_conntrack_info *ctinfo, u_int8_t 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 if (LOG_INVALID(net, IPPROTO_ICMP))
190 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
191 "nf_ct_icmp: short packet ");
195 /* See ip_conntrack_proto_tcp.c */
196 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
197 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
198 if (LOG_INVALID(net, IPPROTO_ICMP))
199 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
200 "nf_ct_icmp: bad HW ICMP checksum ");
205 * 18 is the highest 'known' ICMP type. Anything else is a mystery
207 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
210 if (icmph->type > NR_ICMP_TYPES) {
211 if (LOG_INVALID(net, IPPROTO_ICMP))
212 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
213 "nf_ct_icmp: invalid ICMP type ");
217 /* Need to track icmp error message? */
218 if (icmph->type != ICMP_DEST_UNREACH &&
219 icmph->type != ICMP_SOURCE_QUENCH &&
220 icmph->type != ICMP_TIME_EXCEEDED &&
221 icmph->type != ICMP_PARAMETERPROB &&
222 icmph->type != ICMP_REDIRECT)
225 return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
228 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
230 #include <linux/netfilter/nfnetlink.h>
231 #include <linux/netfilter/nfnetlink_conntrack.h>
233 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
234 const struct nf_conntrack_tuple *t)
236 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
237 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
238 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
239 goto nla_put_failure;
246 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
247 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
248 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
249 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
252 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
253 struct nf_conntrack_tuple *tuple)
255 if (!tb[CTA_PROTO_ICMP_TYPE] ||
256 !tb[CTA_PROTO_ICMP_CODE] ||
257 !tb[CTA_PROTO_ICMP_ID])
260 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
261 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
262 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
264 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
265 !invmap[tuple->dst.u.icmp.type])
271 static int icmp_nlattr_tuple_size(void)
273 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
277 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
279 #include <linux/netfilter/nfnetlink.h>
280 #include <linux/netfilter/nfnetlink_cttimeout.h>
282 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
283 struct net *net, void *data)
285 unsigned int *timeout = data;
286 struct nf_icmp_net *in = icmp_pernet(net);
288 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
290 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
292 /* Set default ICMP timeout. */
293 *timeout = in->timeout;
299 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
301 const unsigned int *timeout = data;
303 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
304 goto nla_put_failure;
311 static const struct nla_policy
312 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
313 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
315 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
318 static struct ctl_table icmp_sysctl_table[] = {
320 .procname = "nf_conntrack_icmp_timeout",
321 .maxlen = sizeof(unsigned int),
323 .proc_handler = proc_dointvec_jiffies,
327 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
328 static struct ctl_table icmp_compat_sysctl_table[] = {
330 .procname = "ip_conntrack_icmp_timeout",
331 .maxlen = sizeof(unsigned int),
333 .proc_handler = proc_dointvec_jiffies,
337 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
338 #endif /* CONFIG_SYSCTL */
340 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
341 struct nf_icmp_net *in)
344 pn->ctl_table = kmemdup(icmp_sysctl_table,
345 sizeof(icmp_sysctl_table),
350 pn->ctl_table[0].data = &in->timeout;
355 static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
356 struct nf_icmp_net *in)
359 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
360 pn->ctl_compat_table = kmemdup(icmp_compat_sysctl_table,
361 sizeof(icmp_compat_sysctl_table),
363 if (!pn->ctl_compat_table)
366 pn->ctl_compat_table[0].data = &in->timeout;
372 static int icmp_init_net(struct net *net, u_int16_t proto)
375 struct nf_icmp_net *in = icmp_pernet(net);
376 struct nf_proto_net *pn = &in->pn;
378 in->timeout = nf_ct_icmp_timeout;
380 ret = icmp_kmemdup_compat_sysctl_table(pn, in);
384 ret = icmp_kmemdup_sysctl_table(pn, in);
386 nf_ct_kfree_compat_sysctl_table(pn);
391 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
393 return &net->ct.nf_ct_proto.icmp.pn;
396 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
399 .l4proto = IPPROTO_ICMP,
401 .pkt_to_tuple = icmp_pkt_to_tuple,
402 .invert_tuple = icmp_invert_tuple,
403 .print_tuple = icmp_print_tuple,
404 .packet = icmp_packet,
405 .get_timeouts = icmp_get_timeouts,
410 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
411 .tuple_to_nlattr = icmp_tuple_to_nlattr,
412 .nlattr_tuple_size = icmp_nlattr_tuple_size,
413 .nlattr_to_tuple = icmp_nlattr_to_tuple,
414 .nla_policy = icmp_nla_policy,
416 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
418 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
419 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
420 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
421 .obj_size = sizeof(unsigned int),
422 .nla_policy = icmp_timeout_nla_policy,
424 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
425 .init_net = icmp_init_net,
426 .get_net_proto = icmp_get_net_proto,