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 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 bool nf_conntrack_invert_icmp_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 int nf_conntrack_icmp_packet(struct nf_conn *ct,
73 enum ip_conntrack_info ctinfo,
74 const struct nf_hook_state *state)
76 /* Do not immediately delete the connection after the first
77 successful reply to avoid excessive conntrackd traffic
78 and also to handle correctly ICMP echo reply duplicates. */
79 unsigned int *timeout = nf_ct_timeout_lookup(ct);
80 static const u_int8_t valid_new[] = {
83 [ICMP_INFO_REQUEST] = 1,
87 if (state->pf != NFPROTO_IPV4)
90 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
91 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
92 /* Can't create a new ICMP `conn' with this. */
93 pr_debug("icmp: can't create new conn with type %u\n",
94 ct->tuplehash[0].tuple.dst.u.icmp.type);
95 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
100 timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout;
102 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
106 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
108 icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
109 const struct nf_hook_state *state)
111 struct nf_conntrack_tuple innertuple, origtuple;
112 const struct nf_conntrack_tuple_hash *h;
113 const struct nf_conntrack_zone *zone;
114 enum ip_conntrack_info ctinfo;
115 struct nf_conntrack_zone tmp;
117 WARN_ON(skb_nfct(skb));
118 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
120 /* Are they talking about one of our connections? */
121 if (!nf_ct_get_tuplepr(skb,
122 skb_network_offset(skb) + ip_hdrlen(skb)
123 + sizeof(struct icmphdr),
124 PF_INET, state->net, &origtuple)) {
125 pr_debug("icmp_error_message: failed to get tuple\n");
129 /* Ordinarily, we'd expect the inverted tupleproto, but it's
130 been preserved inside the ICMP. */
131 if (!nf_ct_invert_tuple(&innertuple, &origtuple)) {
132 pr_debug("icmp_error_message: no match\n");
136 ctinfo = IP_CT_RELATED;
138 h = nf_conntrack_find_get(state->net, zone, &innertuple);
140 pr_debug("icmp_error_message: no match\n");
144 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
145 ctinfo += IP_CT_IS_REPLY;
147 /* Update skb to refer to this connection */
148 nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
152 static void icmp_error_log(const struct sk_buff *skb,
153 const struct nf_hook_state *state,
156 nf_l4proto_log_invalid(skb, state->net, state->pf,
157 IPPROTO_ICMP, "%s", msg);
160 /* Small and modified version of icmp_rcv */
161 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
162 struct sk_buff *skb, unsigned int dataoff,
163 const struct nf_hook_state *state)
165 const struct icmphdr *icmph;
168 /* Not enough header? */
169 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
171 icmp_error_log(skb, state, "short packet");
175 /* See ip_conntrack_proto_tcp.c */
176 if (state->net->ct.sysctl_checksum &&
177 state->hook == NF_INET_PRE_ROUTING &&
178 nf_ip_checksum(skb, state->hook, dataoff, 0)) {
179 icmp_error_log(skb, state, "bad hw icmp checksum");
184 * 18 is the highest 'known' ICMP type. Anything else is a mystery
186 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
189 if (icmph->type > NR_ICMP_TYPES) {
190 icmp_error_log(skb, state, "invalid icmp type");
194 /* Need to track icmp error message? */
195 if (icmph->type != ICMP_DEST_UNREACH &&
196 icmph->type != ICMP_SOURCE_QUENCH &&
197 icmph->type != ICMP_TIME_EXCEEDED &&
198 icmph->type != ICMP_PARAMETERPROB &&
199 icmph->type != ICMP_REDIRECT)
202 return icmp_error_message(tmpl, skb, state);
205 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
207 #include <linux/netfilter/nfnetlink.h>
208 #include <linux/netfilter/nfnetlink_conntrack.h>
210 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
211 const struct nf_conntrack_tuple *t)
213 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
214 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
215 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
216 goto nla_put_failure;
223 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
224 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
225 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
226 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
229 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
230 struct nf_conntrack_tuple *tuple)
232 if (!tb[CTA_PROTO_ICMP_TYPE] ||
233 !tb[CTA_PROTO_ICMP_CODE] ||
234 !tb[CTA_PROTO_ICMP_ID])
237 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
238 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
239 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
241 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
242 !invmap[tuple->dst.u.icmp.type])
248 static unsigned int icmp_nlattr_tuple_size(void)
250 static unsigned int size __read_mostly;
253 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
259 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
261 #include <linux/netfilter/nfnetlink.h>
262 #include <linux/netfilter/nfnetlink_cttimeout.h>
264 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
265 struct net *net, void *data)
267 unsigned int *timeout = data;
268 struct nf_icmp_net *in = nf_icmp_pernet(net);
270 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
272 timeout = &in->timeout;
274 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
275 } else if (timeout) {
276 /* Set default ICMP timeout. */
277 *timeout = in->timeout;
283 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
285 const unsigned int *timeout = data;
287 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
288 goto nla_put_failure;
295 static const struct nla_policy
296 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
297 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
299 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
301 void nf_conntrack_icmp_init_net(struct net *net)
303 struct nf_icmp_net *in = nf_icmp_pernet(net);
305 in->timeout = nf_ct_icmp_timeout;
308 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
310 .l4proto = IPPROTO_ICMP,
311 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
312 .tuple_to_nlattr = icmp_tuple_to_nlattr,
313 .nlattr_tuple_size = icmp_nlattr_tuple_size,
314 .nlattr_to_tuple = icmp_nlattr_to_tuple,
315 .nla_policy = icmp_nla_policy,
317 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
319 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
320 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
321 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
322 .obj_size = sizeof(unsigned int),
323 .nla_policy = icmp_timeout_nla_policy,
325 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */