ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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 unsigned long ip_ct_icmp_timeout = 30*HZ;
18
19 #if 0
20 #define DEBUGP printk
21 #else
22 #define DEBUGP(format, args...)
23 #endif
24
25 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
26                              unsigned int dataoff,
27                              struct ip_conntrack_tuple *tuple)
28 {
29         struct icmphdr hdr;
30
31         if (skb_copy_bits(skb, dataoff, &hdr, sizeof(hdr)) != 0)
32                 return 0;
33
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;
37
38         return 1;
39 }
40
41 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
42                              const struct ip_conntrack_tuple *orig)
43 {
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};
54
55         if (orig->dst.u.icmp.type >= sizeof(invmap)
56             || !invmap[orig->dst.u.icmp.type])
57                 return 0;
58
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;
62         return 1;
63 }
64
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)
68 {
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));
73 }
74
75 /* Print out the private part of the conntrack. */
76 static unsigned int icmp_print_conntrack(char *buffer,
77                                      const struct ip_conntrack *conntrack)
78 {
79         return 0;
80 }
81
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)
86 {
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);
95         } else {
96                 atomic_inc(&ct->proto.icmp.count);
97                 ip_ct_refresh(ct, ip_ct_icmp_timeout);
98         }
99
100         return NF_ACCEPT;
101 }
102
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)
106 {
107         static u_int8_t valid_new[]
108                 = { [ICMP_ECHO] = 1,
109                     [ICMP_TIMESTAMP] = 1,
110                     [ICMP_INFO_REQUEST] = 1,
111                     [ICMP_ADDRESS] = 1 };
112
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);
119                 return 0;
120         }
121         atomic_set(&conntrack->proto.icmp.count, 0);
122         return 1;
123 }
124
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 };