www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / net / ipv4 / netfilter / ip_conntrack_proto_icmp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
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.
7  */
8
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <linux/in.h>
14 #include <linux/icmp.h>
15 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
16
17 #ifdef CONFIG_MIPS_BRCM
18 unsigned long ip_ct_icmp_timeout = 4*HZ;
19 #else
20 unsigned long ip_ct_icmp_timeout = 30*HZ;
21 #endif
22
23 #if 0
24 #define DEBUGP printk
25 #else
26 #define DEBUGP(format, args...)
27 #endif
28
29 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
30                              unsigned int dataoff,
31                              struct ip_conntrack_tuple *tuple)
32 {
33         struct icmphdr hdr;
34
35         if (skb_copy_bits(skb, dataoff, &hdr, sizeof(hdr)) != 0)
36                 return 0;
37
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;
41
42         return 1;
43 }
44
45 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
46                              const struct ip_conntrack_tuple *orig)
47 {
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};
58
59         if (orig->dst.u.icmp.type >= sizeof(invmap)
60             || !invmap[orig->dst.u.icmp.type])
61                 return 0;
62
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;
66         return 1;
67 }
68
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)
72 {
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));
77 }
78
79 /* Print out the private part of the conntrack. */
80 static unsigned int icmp_print_conntrack(char *buffer,
81                                      const struct ip_conntrack *conntrack)
82 {
83         return 0;
84 }
85
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)
90 {
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);
99         } else {
100                 atomic_inc(&ct->proto.icmp.count);
101                 ip_ct_refresh(ct, ip_ct_icmp_timeout);
102         }
103
104         return NF_ACCEPT;
105 }
106
107 #ifdef CONFIG_MIPS_BRCM
108 extern int ip_conntrack_max;
109 #endif
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)
113 {
114         static u_int8_t valid_new[]
115                 = { [ICMP_ECHO] = 1,
116                     [ICMP_TIMESTAMP] = 1,
117                     [ICMP_INFO_REQUEST] = 1,
118                     [ICMP_ADDRESS] = 1 };
119 #ifdef CONFIG_MIPS_BRCM
120         if ( ip_conntrack_max == 0)
121                 return NF_DROP;
122 #endif
123
124
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);
131                 return 0;
132         }
133         atomic_set(&conntrack->proto.icmp.count, 0);
134         return 1;
135 }
136
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 };