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 #ifdef CONFIG_MIPS_BRCM
18 unsigned long ip_ct_icmp_timeout = 4*HZ;
20 unsigned long ip_ct_icmp_timeout = 30*HZ;
26 #define DEBUGP(format, args...)
29 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
31 struct ip_conntrack_tuple *tuple)
35 if (skb_copy_bits(skb, dataoff, &hdr, sizeof(hdr)) != 0)
38 tuple->dst.u.icmp.type = hdr.type;
39 tuple->src.u.icmp.id = hdr.un.echo.id;
40 tuple->dst.u.icmp.code = hdr.code;
45 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
46 const struct ip_conntrack_tuple *orig)
48 /* Add 1; spaces filled with 0. */
49 static 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};
59 if (orig->dst.u.icmp.type >= sizeof(invmap)
60 || !invmap[orig->dst.u.icmp.type])
63 tuple->src.u.icmp.id = orig->src.u.icmp.id;
64 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
65 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
69 /* Print out the per-protocol part of the tuple. */
70 static unsigned int icmp_print_tuple(char *buffer,
71 const struct ip_conntrack_tuple *tuple)
73 return sprintf(buffer, "type=%u code=%u id=%u ",
74 tuple->dst.u.icmp.type,
75 tuple->dst.u.icmp.code,
76 ntohs(tuple->src.u.icmp.id));
79 /* Print out the private part of the conntrack. */
80 static unsigned int icmp_print_conntrack(char *buffer,
81 const struct ip_conntrack *conntrack)
86 /* Returns verdict for packet, or -1 for invalid. */
87 static int icmp_packet(struct ip_conntrack *ct,
88 const struct sk_buff *skb,
89 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 ip_ct_refresh(ct, ip_ct_icmp_timeout);
107 #ifdef CONFIG_MIPS_BRCM
108 extern int ip_conntrack_max;
110 /* Called when a new connection for this protocol found. */
111 static int icmp_new(struct ip_conntrack *conntrack,
112 const struct sk_buff *skb)
114 static u_int8_t valid_new[]
116 [ICMP_TIMESTAMP] = 1,
117 [ICMP_INFO_REQUEST] = 1,
118 [ICMP_ADDRESS] = 1 };
119 #ifdef CONFIG_MIPS_BRCM
120 if ( ip_conntrack_max == 0)
125 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
126 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
127 /* Can't create a new ICMP `conn' with this. */
128 DEBUGP("icmp: can't create new conn with type %u\n",
129 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
130 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
133 atomic_set(&conntrack->proto.icmp.count, 0);
137 struct ip_conntrack_protocol ip_conntrack_protocol_icmp
138 = { { NULL, NULL }, IPPROTO_ICMP, "icmp",
139 icmp_pkt_to_tuple, icmp_invert_tuple, icmp_print_tuple,
140 icmp_print_conntrack, icmp_packet, icmp_new, NULL, NULL, NULL };