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.
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - enable working with Layer 3 protocol independent connection tracking.
11 * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c
14 #include <linux/types.h>
15 #include <linux/timer.h>
16 #include <linux/netfilter.h>
18 #include <linux/icmp.h>
19 #include <linux/seq_file.h>
21 #include <net/checksum.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <net/netfilter/nf_conntrack_tuple.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_core.h>
27 static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
32 #define DEBUGP(format, args...)
35 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
37 struct nf_conntrack_tuple *tuple)
39 struct icmphdr _hdr, *hp;
41 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
45 tuple->dst.u.icmp.type = hp->type;
46 tuple->src.u.icmp.id = hp->un.echo.id;
47 tuple->dst.u.icmp.code = hp->code;
52 /* Add 1; spaces filled with 0. */
53 static const u_int8_t invmap[] = {
54 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
55 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
56 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
57 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
58 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
59 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
60 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
61 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
64 static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
65 const struct nf_conntrack_tuple *orig)
67 if (orig->dst.u.icmp.type >= sizeof(invmap)
68 || !invmap[orig->dst.u.icmp.type])
71 tuple->src.u.icmp.id = orig->src.u.icmp.id;
72 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
73 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
77 /* Print out the per-protocol part of the tuple. */
78 static int icmp_print_tuple(struct seq_file *s,
79 const struct nf_conntrack_tuple *tuple)
81 return seq_printf(s, "type=%u code=%u id=%u ",
82 tuple->dst.u.icmp.type,
83 tuple->dst.u.icmp.code,
84 ntohs(tuple->src.u.icmp.id));
87 /* Print out the private part of the conntrack. */
88 static int icmp_print_conntrack(struct seq_file *s,
89 const struct nf_conn *conntrack)
94 /* Returns verdict for packet, or -1 for invalid. */
95 static int icmp_packet(struct nf_conn *ct,
96 const struct sk_buff *skb,
98 enum ip_conntrack_info ctinfo,
100 unsigned int hooknum)
102 /* Try to delete connection immediately after all replies:
103 won't actually vanish as we still have skb, and del_timer
104 means this will only run once even if count hits zero twice
105 (theoretically possible with SMP) */
106 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
107 if (atomic_dec_and_test(&ct->proto.icmp.count)
108 && del_timer(&ct->timeout))
109 ct->timeout.function((unsigned long)ct);
111 atomic_inc(&ct->proto.icmp.count);
112 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
113 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
119 /* Called when a new connection for this protocol found. */
120 static int icmp_new(struct nf_conn *conntrack,
121 const struct sk_buff *skb, unsigned int dataoff)
123 static const u_int8_t valid_new[] = {
125 [ICMP_TIMESTAMP] = 1,
126 [ICMP_INFO_REQUEST] = 1,
130 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
131 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
132 /* Can't create a new ICMP `conn' with this. */
133 DEBUGP("icmp: can't create new conn with type %u\n",
134 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
135 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
138 atomic_set(&conntrack->proto.icmp.count, 0);
142 extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
143 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
145 icmp_error_message(struct sk_buff *skb,
146 enum ip_conntrack_info *ctinfo,
147 unsigned int hooknum)
149 struct nf_conntrack_tuple innertuple, origtuple;
154 struct nf_conntrack_l4proto *innerproto;
155 struct nf_conntrack_tuple_hash *h;
158 NF_CT_ASSERT(skb->nfct == NULL);
160 /* Not enough header? */
161 inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
165 /* Ignore ICMP's containing fragments (shouldn't happen) */
166 if (inside->ip.frag_off & htons(IP_OFFSET)) {
167 DEBUGP("icmp_error_message: fragment of proto %u\n",
168 inside->ip.protocol);
172 /* rcu_read_lock()ed by nf_hook_slow */
173 innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
175 dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp);
176 /* Are they talking about one of our connections? */
177 if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET,
178 inside->ip.protocol, &origtuple,
179 &nf_conntrack_l3proto_ipv4, innerproto)) {
180 DEBUGP("icmp_error_message: ! get_tuple p=%u",
181 inside->ip.protocol);
185 /* Ordinarily, we'd expect the inverted tupleproto, but it's
186 been preserved inside the ICMP. */
187 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
188 &nf_conntrack_l3proto_ipv4, innerproto)) {
189 DEBUGP("icmp_error_message: no match\n");
193 *ctinfo = IP_CT_RELATED;
195 h = nf_conntrack_find_get(&innertuple, NULL);
197 /* Locally generated ICMPs will match inverted if they
198 haven't been SNAT'ed yet */
199 /* FIXME: NAT code has to handle half-done double NAT --RR */
200 if (hooknum == NF_IP_LOCAL_OUT)
201 h = nf_conntrack_find_get(&origtuple, NULL);
204 DEBUGP("icmp_error_message: no match\n");
208 /* Reverse direction from that found */
209 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
210 *ctinfo += IP_CT_IS_REPLY;
212 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
213 *ctinfo += IP_CT_IS_REPLY;
216 /* Update skb to refer to this connection */
217 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
218 skb->nfctinfo = *ctinfo;
222 /* Small and modified version of icmp_rcv */
224 icmp_error(struct sk_buff *skb, unsigned int dataoff,
225 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
227 struct icmphdr _ih, *icmph;
229 /* Not enough header? */
230 icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
232 if (LOG_INVALID(IPPROTO_ICMP))
233 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
234 "nf_ct_icmp: short packet ");
238 /* See ip_conntrack_proto_tcp.c */
239 if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
240 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
241 if (LOG_INVALID(IPPROTO_ICMP))
242 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
243 "nf_ct_icmp: bad HW ICMP checksum ");
248 * 18 is the highest 'known' ICMP type. Anything else is a mystery
250 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
253 if (icmph->type > NR_ICMP_TYPES) {
254 if (LOG_INVALID(IPPROTO_ICMP))
255 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
256 "nf_ct_icmp: invalid ICMP type ");
260 /* Need to track icmp error message? */
261 if (icmph->type != ICMP_DEST_UNREACH
262 && icmph->type != ICMP_SOURCE_QUENCH
263 && icmph->type != ICMP_TIME_EXCEEDED
264 && icmph->type != ICMP_PARAMETERPROB
265 && icmph->type != ICMP_REDIRECT)
268 return icmp_error_message(skb, ctinfo, hooknum);
271 #if defined(CONFIG_NF_CT_NETLINK) || \
272 defined(CONFIG_NF_CT_NETLINK_MODULE)
274 #include <linux/netfilter/nfnetlink.h>
275 #include <linux/netfilter/nfnetlink_conntrack.h>
277 static int icmp_tuple_to_nfattr(struct sk_buff *skb,
278 const struct nf_conntrack_tuple *t)
280 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
282 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
283 &t->dst.u.icmp.type);
284 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
285 &t->dst.u.icmp.code);
293 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
294 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
295 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
296 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
299 static int icmp_nfattr_to_tuple(struct nfattr *tb[],
300 struct nf_conntrack_tuple *tuple)
302 if (!tb[CTA_PROTO_ICMP_TYPE-1]
303 || !tb[CTA_PROTO_ICMP_CODE-1]
304 || !tb[CTA_PROTO_ICMP_ID-1])
307 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
310 tuple->dst.u.icmp.type =
311 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
312 tuple->dst.u.icmp.code =
313 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
314 tuple->src.u.icmp.id =
315 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
317 if (tuple->dst.u.icmp.type >= sizeof(invmap)
318 || !invmap[tuple->dst.u.icmp.type])
326 static struct ctl_table_header *icmp_sysctl_header;
327 static struct ctl_table icmp_sysctl_table[] = {
329 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
330 .procname = "nf_conntrack_icmp_timeout",
331 .data = &nf_ct_icmp_timeout,
332 .maxlen = sizeof(unsigned int),
334 .proc_handler = &proc_dointvec_jiffies,
340 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
341 static struct ctl_table icmp_compat_sysctl_table[] = {
343 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
344 .procname = "ip_conntrack_icmp_timeout",
345 .data = &nf_ct_icmp_timeout,
346 .maxlen = sizeof(unsigned int),
348 .proc_handler = &proc_dointvec_jiffies,
354 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
355 #endif /* CONFIG_SYSCTL */
357 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
360 .l4proto = IPPROTO_ICMP,
362 .pkt_to_tuple = icmp_pkt_to_tuple,
363 .invert_tuple = icmp_invert_tuple,
364 .print_tuple = icmp_print_tuple,
365 .print_conntrack = icmp_print_conntrack,
366 .packet = icmp_packet,
371 #if defined(CONFIG_NF_CT_NETLINK) || \
372 defined(CONFIG_NF_CT_NETLINK_MODULE)
373 .tuple_to_nfattr = icmp_tuple_to_nfattr,
374 .nfattr_to_tuple = icmp_nfattr_to_tuple,
377 .ctl_table_header = &icmp_sysctl_header,
378 .ctl_table = icmp_sysctl_table,
379 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
380 .ctl_compat_table = icmp_compat_sysctl_table,
384 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);