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>
22 static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
24 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
26 struct nf_conntrack_tuple *tuple)
28 struct icmphdr _hdr, *hp;
30 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
34 tuple->dst.u.icmp.type = hp->type;
35 tuple->src.u.icmp.id = hp->un.echo.id;
36 tuple->dst.u.icmp.code = hp->code;
41 /* Add 1; spaces filled with 0. */
42 static const u_int8_t invmap[] = {
43 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
44 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
45 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
46 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
47 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
48 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
49 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
50 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
53 static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
54 const struct nf_conntrack_tuple *orig)
56 if (orig->dst.u.icmp.type >= sizeof(invmap)
57 || !invmap[orig->dst.u.icmp.type])
60 tuple->src.u.icmp.id = orig->src.u.icmp.id;
61 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
62 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
66 /* Print out the per-protocol part of the tuple. */
67 static int icmp_print_tuple(struct seq_file *s,
68 const struct nf_conntrack_tuple *tuple)
70 return seq_printf(s, "type=%u code=%u id=%u ",
71 tuple->dst.u.icmp.type,
72 tuple->dst.u.icmp.code,
73 ntohs(tuple->src.u.icmp.id));
76 /* Print out the private part of the conntrack. */
77 static int icmp_print_conntrack(struct seq_file *s,
78 const struct nf_conn *conntrack)
83 /* Returns verdict for packet, or -1 for invalid. */
84 static int icmp_packet(struct nf_conn *ct,
85 const struct sk_buff *skb,
87 enum ip_conntrack_info ctinfo,
91 /* Try to delete connection immediately after all replies:
92 won't actually vanish as we still have skb, and del_timer
93 means this will only run once even if count hits zero twice
94 (theoretically possible with SMP) */
95 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
96 if (atomic_dec_and_test(&ct->proto.icmp.count)
97 && del_timer(&ct->timeout))
98 ct->timeout.function((unsigned long)ct);
100 atomic_inc(&ct->proto.icmp.count);
101 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
102 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
108 /* Called when a new connection for this protocol found. */
109 static int icmp_new(struct nf_conn *conntrack,
110 const struct sk_buff *skb, unsigned int dataoff)
112 static const u_int8_t valid_new[] = {
114 [ICMP_TIMESTAMP] = 1,
115 [ICMP_INFO_REQUEST] = 1,
119 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
120 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
121 /* Can't create a new ICMP `conn' with this. */
122 pr_debug("icmp: can't create new conn with type %u\n",
123 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
124 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
127 atomic_set(&conntrack->proto.icmp.count, 0);
131 extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
132 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
134 icmp_error_message(struct sk_buff *skb,
135 enum ip_conntrack_info *ctinfo,
136 unsigned int hooknum)
138 struct nf_conntrack_tuple innertuple, origtuple;
139 struct nf_conntrack_l4proto *innerproto;
140 struct nf_conntrack_tuple_hash *h;
142 NF_CT_ASSERT(skb->nfct == NULL);
144 /* Are they talking about one of our connections? */
145 if (!nf_ct_get_tuplepr(skb,
146 skb_network_offset(skb) + ip_hdrlen(skb)
147 + sizeof(struct icmphdr),
148 PF_INET, &origtuple)) {
149 pr_debug("icmp_error_message: failed to get tuple\n");
153 /* rcu_read_lock()ed by nf_hook_slow */
154 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
156 /* Ordinarily, we'd expect the inverted tupleproto, but it's
157 been preserved inside the ICMP. */
158 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
159 &nf_conntrack_l3proto_ipv4, innerproto)) {
160 pr_debug("icmp_error_message: no match\n");
164 *ctinfo = IP_CT_RELATED;
166 h = nf_conntrack_find_get(&innertuple);
168 /* Locally generated ICMPs will match inverted if they
169 haven't been SNAT'ed yet */
170 /* FIXME: NAT code has to handle half-done double NAT --RR */
171 if (hooknum == NF_IP_LOCAL_OUT)
172 h = nf_conntrack_find_get(&origtuple);
175 pr_debug("icmp_error_message: no match\n");
179 /* Reverse direction from that found */
180 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
181 *ctinfo += IP_CT_IS_REPLY;
183 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
184 *ctinfo += IP_CT_IS_REPLY;
187 /* Update skb to refer to this connection */
188 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
189 skb->nfctinfo = *ctinfo;
193 /* Small and modified version of icmp_rcv */
195 icmp_error(struct sk_buff *skb, unsigned int dataoff,
196 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
198 struct icmphdr _ih, *icmph;
200 /* Not enough header? */
201 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
203 if (LOG_INVALID(IPPROTO_ICMP))
204 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
205 "nf_ct_icmp: short packet ");
209 /* See ip_conntrack_proto_tcp.c */
210 if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
211 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
212 if (LOG_INVALID(IPPROTO_ICMP))
213 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
214 "nf_ct_icmp: bad HW ICMP checksum ");
219 * 18 is the highest 'known' ICMP type. Anything else is a mystery
221 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
224 if (icmph->type > NR_ICMP_TYPES) {
225 if (LOG_INVALID(IPPROTO_ICMP))
226 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
227 "nf_ct_icmp: invalid ICMP type ");
231 /* Need to track icmp error message? */
232 if (icmph->type != ICMP_DEST_UNREACH
233 && icmph->type != ICMP_SOURCE_QUENCH
234 && icmph->type != ICMP_TIME_EXCEEDED
235 && icmph->type != ICMP_PARAMETERPROB
236 && icmph->type != ICMP_REDIRECT)
239 return icmp_error_message(skb, ctinfo, hooknum);
242 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
244 #include <linux/netfilter/nfnetlink.h>
245 #include <linux/netfilter/nfnetlink_conntrack.h>
247 static int icmp_tuple_to_nfattr(struct sk_buff *skb,
248 const struct nf_conntrack_tuple *t)
250 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
252 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
253 &t->dst.u.icmp.type);
254 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
255 &t->dst.u.icmp.code);
263 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
264 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
265 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
266 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
269 static int icmp_nfattr_to_tuple(struct nfattr *tb[],
270 struct nf_conntrack_tuple *tuple)
272 if (!tb[CTA_PROTO_ICMP_TYPE-1]
273 || !tb[CTA_PROTO_ICMP_CODE-1]
274 || !tb[CTA_PROTO_ICMP_ID-1])
277 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
280 tuple->dst.u.icmp.type =
281 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
282 tuple->dst.u.icmp.code =
283 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
284 tuple->src.u.icmp.id =
285 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
287 if (tuple->dst.u.icmp.type >= sizeof(invmap)
288 || !invmap[tuple->dst.u.icmp.type])
296 static struct ctl_table_header *icmp_sysctl_header;
297 static struct ctl_table icmp_sysctl_table[] = {
299 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
300 .procname = "nf_conntrack_icmp_timeout",
301 .data = &nf_ct_icmp_timeout,
302 .maxlen = sizeof(unsigned int),
304 .proc_handler = &proc_dointvec_jiffies,
310 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
311 static struct ctl_table icmp_compat_sysctl_table[] = {
313 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
314 .procname = "ip_conntrack_icmp_timeout",
315 .data = &nf_ct_icmp_timeout,
316 .maxlen = sizeof(unsigned int),
318 .proc_handler = &proc_dointvec_jiffies,
324 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
325 #endif /* CONFIG_SYSCTL */
327 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
330 .l4proto = IPPROTO_ICMP,
332 .pkt_to_tuple = icmp_pkt_to_tuple,
333 .invert_tuple = icmp_invert_tuple,
334 .print_tuple = icmp_print_tuple,
335 .print_conntrack = icmp_print_conntrack,
336 .packet = icmp_packet,
341 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
342 .tuple_to_nfattr = icmp_tuple_to_nfattr,
343 .nfattr_to_tuple = icmp_nfattr_to_tuple,
346 .ctl_table_header = &icmp_sysctl_header,
347 .ctl_table = icmp_sysctl_table,
348 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
349 .ctl_compat_table = icmp_compat_sysctl_table,
353 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);