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/sched.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
14 #include <linux/icmp.h>
15 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
17 unsigned long ip_ct_icmp_timeout = 30*HZ;
22 #define DEBUGP(format, args...)
25 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
27 struct ip_conntrack_tuple *tuple)
31 if (skb_copy_bits(skb, dataoff, &hdr, sizeof(hdr)) != 0)
34 tuple->dst.u.icmp.type = hdr.type;
35 tuple->src.u.icmp.id = hdr.un.echo.id;
36 tuple->dst.u.icmp.code = hdr.code;
41 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
42 const struct ip_conntrack_tuple *orig)
44 /* Add 1; spaces filled with 0. */
45 static u_int8_t invmap[]
46 = { [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
47 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
48 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
49 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
50 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
51 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
52 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
53 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1};
55 if (orig->dst.u.icmp.type >= sizeof(invmap)
56 || !invmap[orig->dst.u.icmp.type])
59 tuple->src.u.icmp.id = orig->src.u.icmp.id;
60 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
61 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
65 /* Print out the per-protocol part of the tuple. */
66 static unsigned int icmp_print_tuple(char *buffer,
67 const struct ip_conntrack_tuple *tuple)
69 return sprintf(buffer, "type=%u code=%u id=%u ",
70 tuple->dst.u.icmp.type,
71 tuple->dst.u.icmp.code,
72 ntohs(tuple->src.u.icmp.id));
75 /* Print out the private part of the conntrack. */
76 static unsigned int icmp_print_conntrack(char *buffer,
77 const struct ip_conntrack *conntrack)
82 /* Returns verdict for packet, or -1 for invalid. */
83 static int icmp_packet(struct ip_conntrack *ct,
84 const struct sk_buff *skb,
85 enum ip_conntrack_info ctinfo)
87 /* Try to delete connection immediately after all replies:
88 won't actually vanish as we still have skb, and del_timer
89 means this will only run once even if count hits zero twice
90 (theoretically possible with SMP) */
91 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
92 if (atomic_dec_and_test(&ct->proto.icmp.count)
93 && del_timer(&ct->timeout))
94 ct->timeout.function((unsigned long)ct);
96 atomic_inc(&ct->proto.icmp.count);
97 ip_ct_refresh(ct, ip_ct_icmp_timeout);
103 /* Called when a new connection for this protocol found. */
104 static int icmp_new(struct ip_conntrack *conntrack,
105 const struct sk_buff *skb)
107 static u_int8_t valid_new[]
109 [ICMP_TIMESTAMP] = 1,
110 [ICMP_INFO_REQUEST] = 1,
111 [ICMP_ADDRESS] = 1 };
113 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
114 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
115 /* Can't create a new ICMP `conn' with this. */
116 DEBUGP("icmp: can't create new conn with type %u\n",
117 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
118 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
121 atomic_set(&conntrack->proto.icmp.count, 0);
125 struct ip_conntrack_protocol ip_conntrack_protocol_icmp
126 = { { NULL, NULL }, IPPROTO_ICMP, "icmp",
127 icmp_pkt_to_tuple, icmp_invert_tuple, icmp_print_tuple,
128 icmp_print_conntrack, icmp_packet, icmp_new, NULL, NULL, NULL };